extra constructor calls, inefficent loop variable scope?

PERL

    Next

  • 1. problems using graphviz perl module from apache (windows)
    I'm writing a cgi script that uses graphviz (the dot program) to generate a graph and I'm banging my head against the wall trying to get it to work properly. Currently, if I run the script directly from the command line, it will work properly and generate my graph (which gets dumped into a file). If I invoke the script as a cgi script within apache, it runs fine until it calls the as_png() function, at which point it will die waiting for a result (the destination file is created but is empty). I'm running SERVER_SOFTWARE="Apache/2.0.52 (Win32)" and version 1.8 of the GraphViz module. If I run a printenv.cgi, I get the following path: PATH="C:\Program Files\Perl\bin\;C:\Program Files\Reflection;C:\PROGRAM FILES\THINKPAD\UTILITIES; C:\Program Files\Java\j2re1.4.2_01\bin;C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem;C:\Oracle\ora81\bin;C:\Program Files\Oracle\jre\1.1.7\bin;C:\ISIS;C:\PROGRA~1\COMMON~1\MDL Shared\ISIS;C:\PROGRA~1\ATT\Graphviz\bin;C:\PROGRA~1\ATT\Graphviz\bin\tools" which does seem to have the correct path settings for dot.exe. The error that the apache log gives me is: Premature end of script headers: simplegraph.pl If I remove the line that calls as_png(), the script runs fine. Any assistance would be most appreciated. I've searched usenet but to no avail... Thanks! Sergio.
  • 2. Number::Format installation problem
    Hello, I wanted to install Number::Format module bud ended up with following error message. Does anybody have any suggestion what the cause could be?? Could that be due to presence of perldl in my comp? Martin [root@localhost Number-Format-1.45]# make cp Format.pm blib/lib/Number/Format.pm Manifying blib/man3/Number::Format.3pm [root@localhost Number-Format-1.45]# make test PERL_DL_NONLAZY=1 /usr/bin/perl5.8.5 "-MExtUtils::Command::MM" "-e" "test_harness(0, 'blib/lib', 'blib/arch')" t/*.t t/format_bytes.......ok t/format_negative....ok t/format_number......FAILED tests 2-5 Failed 4/9 tests, 55.56% okay t/format_picture.....FAILED tests 2, 8-11 Failed 5/13 tests, 61.54% okay t/format_price.......FAILED tests 2-6 Failed 5/6 tests, 16.67% okay t/locale.............ok t/object.............ok t/round..............ok t/unformat_number....ok Failed Test Stat Wstat Total Fail Failed List of Failed ------------------------------------------------------------------------------- t/format_number.t 9 4 44.44% 2-5 t/format_picture.t 13 5 38.46% 2 8-11 t/format_price.t 6 5 83.33% 2-6 Failed 3/9 test scripts, 66.67% okay. 14/64 subtests failed, 78.12% okay. make: *** [test_dynamic] Error 255
  • 3. strange behavior when using 'read'
    Hi, I have a web/cgi script that allows users to upload a file to the web server. I want to only allow files up to a certain size, which is stored in $imageFileMaxSize (typically 75K). That part works correctly, i.e. the script does not allow uploads larger than $imageFileMaxSize. The problem is, when a user attempts to upload a file larger than $imageFileMaxSize, the on_error routine is not called; instead the script reloads from its starting point, with no message. My users find this confusing, sometimes thinking the file has been uploaded, sometimes getting frustrated and trying again. How can I get on_error to display the proper message? Here's my code: $imageFileMaxSize = 75000; $CGI::POST_MAX = $imageFileMaxSize; sub create_new_image_file { my $newImageFile = @_; $file = param('imagefile'); open (SAVE, ">$newImageFilenamePath")"); while (read($file,$data,1024)) { print SAVE $data; $imageFileLength += length($data); on_error("Image file is too large; maximum size allowed is $imageFileMaxSize") if($imageFileLength > $imageFileMaxSize); } close(SAVE); on_error("File was not received; check filename.") if($imageFileLength < 1); } thanks! Scott
  • 4. displaying STDOUT containing multiple data entries
    I'm writing a stock quote retrieval program (using the Finance::Quote module) that takes up to ten ticker symbols and displays the company name, price, volume, etc. for each. A row of headers is displayed before the data. For some reason, only the headers are being displayed after the ticker symbols are input. I'm chomping the STDIN after the entries are made. The ticker symbols are entered into an array. I'm using the same scalars ($tick[$slot] in this case) for the input and display arrays. Could this last thing be the problem? Jeff Louisville, KY

extra constructor calls, inefficent loop variable scope?

Postby ixtahdoom » Sat, 10 Jul 2004 09:29:29 GMT

Hi All,

Does the declaration of a variable impact performance?  For exmaple:

Loop A:

  while (<>) {
     my $something = $_;
  }

Loop B:

  my $something;
  while (<>) {
     $something = $_;
  }

Is either one better w.r.t. performance?

Thanks,
itd

Re: extra constructor calls, inefficent loop variable scope?

Postby Gunnar Hjalmarsson » Sat, 10 Jul 2004 09:34:15 GMT



I have no idea. If you want to know, make use of the Benchmark module.

-- 
Gunnar Hjalmarsson
Email:  http://www.**--****.com/ 

Similar Threads:

1.Calling SUPER::constructor in the constructor

Is it possible to call the constructor that a function inherits from its
parent?  I tried calling SUPER:: and SUPER-> in a constructor and got
errors.  Am i correct in assuming that if I rewrite the constructor that
a copy of the parent object won't be available?

Thanks in advance,

Dan

2.newbie question about scope, variables, declarations of variables and option strict (as in perl)


Hello,

I am trying to understand the syntax error I receive in a code similar to
this.

1 require 'logger'
2
3 log = logger.new  #some other logger settings are ignored.

4 def func
5   log.debug "a statement" # error is reported here when func is called
below
6  # some code
7 end
8
9 #some code continues
10 func


When func is called, an error is reported on line-5 saying that undefine
local variable log etc. I understand that functions create scopes and log is
seen as local variable  which is not defined in that scope. As it is
qualified with no scope operator, interpreter thinks that it is local but
can not find definition of the log before it's usage but also in the
parameter list and I understand that. On the other hand, I can use log
without qualifying it with a scope symbol anywhere in the same file if it is
not in a function. I know that loops, if statements etc are built into the
language and do not create scope. Code blocks inherit the locals. So it is
meaningful that I can use it anywhere else. When I qualify log with $ as
$log, it becomes global and I no longer receive error. I have tried it
qualifying with @ etc. but the received the same error. What I am asking is,
what is scope of log?. What kind of variable is it? It is the local or
instance variable of what, Object? I know that func is private to the
Object. But what about log?  How  can  I  access it in a function without
making it global?

Is there a way to make variables local to a file as perl does with "my".

Is there a strict option that prevents unintended variable creation because
of typos. Is there a way force predeclaration of variables?

Thanks.

3.How to call ancestor's constructor?

I'm trying to write a screen scraper and I need to use inheritance
(according to a response to an earlier post). 

 

Here is the original piece of code:

 

  my $parser = HTML::Parser->new(api_version => 3);

 

Now how do I change this so it is creating an instance of my custom class?

 

Here is my attempt to make a new class:

 

package CrawlCompanyJobPage;

our @ISA = HTML::Parser;

sub new {

  my $invocant = shift;

  my $class = ref($invocant) || $invocant;

  my $self = {  @_ };

  bless($self, $class);

  return $self;

}

 

I don't think I have the ISA statement correct. 

 

How do I pass api_version=>3 along to my ancestor?

 

Thanks,

Siegfried


4.Scope of hashes in loops

I'm sure this is a really basic question, but I can't seem to find the
answer anywhere:

I set up a hash variable that is global to the script.  Then I put in
key-value pairs using scalar variables that are read out of a file in a
"while" loop.  Later on in the program, in another "while" loop, I try to
access the value of the hash for a given key, but I keep getting "Use of
uninitialized value in string at program.pl line XX, etc..."

It seems as though the hash's scope is limited to the first while loop where
it was created.  I don't think this is supposed to be the case.  Any ideas?

Thanks...

-Steve


5.Class::Struct, accessor overrides not called from constructor

Hello all,

I have a question regarding Class::Struct. It is a great module for 
creating small, structlike objects.
Recently I ran into a problem with it, and I'm not sure if I should file 
a bug report, or whether I should consider my usage of it inappropriate.
So my question to you is, do you think it's a bug?

Here's the issue: Class::Struct allows you to override the accessors it 
creates, but it doesn't call them in its constructor.
In other words,

	$struct->field('blah');

calls my override, but

	$struct = structure->new('field' => 'blah');

doesn't. Class::Struct simply does

	$r->{'field'} = $init{'field'}

(with a defined check), but it would be more useful if it did

	$r->field($init{'field'})

See the sample code below for details.

#!/usr/bin/perl
use strict;
use warnings;
use Class::Struct;
use Data::Dumper;

### define struct
struct memory => {
	swap	=>	'$',
	free	=>	'$',
};

### override accessor for free
sub memory::free {
	my $r = shift;
	die 'Too many args to free' if @_ > 1;
	if(@_) {
		my $id = shift;
		if( $id > 100 or $id < 0 ) {
			warn "free `$id` should be between 0 and 100";
		} else {
			$r->{'memory::free'} = $id;
		}
	}

	$r->{'memory::free'};
}

### create object and dump
my $m = memory->new(free=>110, swap => 100);
print Dumper($m);	# free contains 110!

$m->free(120);		# warns and doesn't update
print Dumper($m);

__END__;

Btw, this occurs both with perl 5.6.1 and 5.8.2.
If this you think this is a bug, what would your advice be? Should I 
file one with perlbug?

Thanks,

Rhesa Rozendaal

6. Calling parent constructor

7. foreach loops and lexically scoped loop variables

8. scope of the variable?



Return to PERL

 

Who is online

Users browsing this forum: No registered users and 8 guest