CORE DUMP by script !!
Hi ,nbsp;nbsp;nbsp; One of my scripy (involves GDBM file operations) dumped core.I have the core file, but no idea what can I glean from that. I have tools like dbx and gdb but they need binaries along with the core file. So anyone knows how to decipher the core file of a script.ThanksMadhab
Trouble with variable scoping
In my perl script, I have a global variable called @excludedIPAddresses, declared at the top of the script using my: use strict; use warnings; use Net::Ping; use Net::Netmask; use Net::NBName; use DBM::Deep; # User-configured variable declarations my @subnets = qw# 192.168.0.0/24 192.168.3.0/24 #;# @subnets should only contain CIDR-type subnet address blocks my @excludedIPAddresses = qw# 192.168.0.142 192.168.3.118 #;# @excludedIPAddresses can only handle specific IP addresses for now Further down, I use it in a subroutine: sub ExcludeIPAddress { # Argument(s): IP Address (string) and $subnet (object reference) # Returned: Boolean value corresponding to whether the IP should be excluded # Globals: Uses @excludedIPAddresses (in user-config section) my $ipAddress = shift @_; my $subnet = shift @_; # The next line does not work; I don't know why. local @excludedIPAddresses = @excludedIPAddresses; my $skip = 0; push(@excludedIPAddresses, ($subnet-broadcast(),$subnet-base())); $skip = grep /$ipAddress/, @excludedIPAddresses; # Commented out original working code # foreach my $exclude (@excludedIPAddresses) #{ #$skip = 1 if ($ipAddress eq $exclude); #} return($skip); } When I run this, I get an error Can't localize lexical variable. I understand that it's because the variable is declared using my; what I don't understand is why, or what I should declare the variable as, since if I leave out my I get an error using use strict. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: [Bulk] RE: Totally lost - need a starting point
Yes I have a perl script running inside of the expect script, which works fine. I need to find a way to call the expect script and output the perl to the html page that is calling it. I found a version of expect for perl, but it didn't seem to be able to get the results that I was looking for, being able to telnet into a terminal and offer up passwords and such. then run another perl script that was picked as an option. Helen - Original Message - From: Charles K. Clarkson [EMAIL PROTECTED] To: beginners@perl.org Sent: Wednesday, August 30, 2006 10:16 PM Subject: [Bulk] RE: Totally lost - need a starting point Helen wrote: : I am starting from scratch again reading the manual more : completely. I am just running out of time on my deadline. Break down your task into small pieces and solve for those pieces. The first piece I can see is running an Expect script from a perl program. Do you know how to do that? I have no idea what Expect is, but searching for 'expect' and 'perl' in Google revealed a perl module for expect. The other pieces of your problem might include getting form values from an HTML form. CGI.pm can help with that. HTH, Charles K. Clarkson -- Mobile Homes Specialist Free Market Advocate Web Programmer 254 968-8328 Don't tread on my bandwidth. Trim your posts. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Trouble with variable scoping
When I run this, I get an error Can't localize lexical variable. I understand that it's because the variable is declared using my; what I don't understand is why, or what I should declare the variable as, since if I leave out my I get an error using use strict. Maybe you want.. Use vars qw(@excludedIPAddresses); Hope this helps... jwm -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: CORE DUMP by script !!
On 31 Aug 2006 07:44:51 -, neela madhab patro [EMAIL PROTECTED] wrote: One of my scripy (involves GDBM file operations) dumped core.I have the core file, but no idea what can I glean from that. The core file is useful if you're debugging perl itself, but generally not useful for debugging your Perl programs. So just fix the bugs in your script, and you can discard any core files you get along the way. If you find out what you're doing that causes the core dump, though, you could file a bug report via perlbug; perl's not supposed to dump core under normal circumstances. Cheers! --Tom Phoenix Stonehenge Perl Training -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: CORE DUMP by script !!
On 31 Aug 2006 07:44:51 -, neela madhab patro [EMAIL PROTECTED] wrote: Hi ,nbsp;nbsp;nbsp; One of my scripy (involves GDBM file operations) dumped core.I have the core file, but no idea what can I glean from that. I have tools like dbx and gdb but they need binaries along with the core file. So anyone knows how to decipher the core file of a script.ThanksMadhab You prbably can't glean much from it. Check the docs, though, particularly perlport and any system-specific docs for your platform (they're all listed at the bottom of perlport). You haven't told us what platform you're running on, but I know, for instance, that there are issues with GDBM and threaded Perls on HP-UX (the fix iirc is to rebuild GDBM, but read the docs). There may be other issues in the docs. HTH, -- jay -- This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] private and confidential daggerquill [at] gmail [dot] com http://www.tuaw.com http://www.downloadsquad.com http://www.engatiki.org values of β will give rise to dom!
File locking issue
Hi, I am trying to save data in an xml file. What I am seeing is when I enable the file locking commands the file is completely wiped out.Just zero bytes. But it works correctly when I remove the file locking commands. Please help ?? What I am doing wrong . Regards, Alok use XML::Twig; use Fcntl qw(:DEFAULT :flock); my $registerFile = Register.xml ; my $twig = XML::Twig-new( pretty_print = 'indented' ); # Save data into the xml file my $node = XML::Twig::Elt-new( 'User', {'name' = $usr}, XML::Twig::Elt-new( 'email' = $email )) ; $node-paste( last_child = $twig-root ) ; #use lock to write into the file sysopen( FH, $registerFile, O_RDWR ) or die can't open $registerFile: $!; flock( FH, LOCK_EX) or die can't lock filename: $!; truncate(FH, 0) or die can't truncate filename: $!; $twig-parsefile( $registerFile ); $twig-print_to_file( $registerFile ) ; close(FH) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Trouble with variable scoping
Roman Daszczyszak wrote: : In my perl script, I have a global variable called : @excludedIPAddresses, declared at the top of the script using : my. That sounds like a bad idea, but go on. : When I run this, I get an error Can't localize lexical : variable. I understand that it's because the variable is : declared using my; what I don't understand is why, or what I : should declare the variable as, since if I leave out my I get : an error using use strict. Don't use local() in this circumstance. sub ExcludeIPAddress { my $ipAddress = shift; my $subnet = shift; return scalar grep /$ipAddress/, @excludedIPAddresses, $subnet-broadcast(), $subnet-base(); } HTH, Charles K. Clarkson -- Mobile Homes Specialist Free Market Advocate Web Programmer 254 968-8328 Don't tread on my bandwidth. Trim your posts. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: [Bulk] RE: Totally lost - need a starting point
Helen wrote: : Yes I have a perl script running inside of the expect script, : which works fine. I need to find a way to call the expect : script and output the perl to the html page that is calling it. Perl needs to print to STDOUT to send information to the browser via CGI. Calling an Expect script via CGI is an Expect question, not a perl question. Perhaps you need to ask your question on an Expect mailing list. I did find this on the Expect FAQ. http://expect.nist.gov/FAQ.html#q24 HTH, Charles K. Clarkson -- Mobile Homes Specialist Free Market Advocate Web Programmer 254 968-8328 Don't tread on my bandwidth. Trim your posts. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Trouble with variable scoping
On 08/31/2006 08:19 AM, Roman Daszczyszak wrote: In my perl script, I have a global variable called @excludedIPAddresses, [...] my @excludedIPAddresses = qw# 192.168.0.142 192.168.3.118 #;# [...] local @excludedIPAddresses = @excludedIPAddresses; [...] When I run this, I get an error Can't localize lexical variable. I understand that it's because the variable is declared using my; what I don't understand is why, or what I should declare the variable as, since if I leave out my I get an error using use strict. Declare it using 'our': our @excludedIPAddresses; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Totally lost - need a starting point
On 08/31/2006 08:24 AM, Helen wrote: - Original Message - From: Charles K. Clarkson [EMAIL PROTECTED] To: beginners@perl.org Sent: Wednesday, August 30, 2006 10:16 PM Subject: [Bulk] RE: Totally lost - need a starting point Helen wrote: I am starting from scratch again reading the manual more completely. I am just running out of time on my deadline. Break down your task into small pieces and solve for those pieces. The first piece I can see is running an Expect script from a perl program. Do you know how to do that? I have no idea what Expect is, but searching for 'expect' and 'perl' in Google revealed a perl module for expect. The other pieces of your problem might include getting form values from an HTML form. CGI.pm can help with that. HTH, Charles K. Clarkson Yes I have a perl script running inside of the expect script, which works fine. I need to find a way to call the expect script and output the perl What perl? to the html page that is calling it. I found a version of expect for perl, but it didn't seem to be able to get the results that I was looking for, being able to telnet into a terminal and offer up passwords and such. then run another perl script that was picked as an option. Helen Hello Helen. First, you're confusing me; please don't top-post, and [Bulk] RE: should not have been added to the subject line; tame your mail-reader. Second, Expect.pm can do telnet: use strict; use warnings; use Expect; my $exp = Expect-spawn(telnet = qw(localhost 80)) or die (Spawn failed: $!\n); my $data = ''; my $tosend = q{GET / HTTP/1.1 Host: localhost Connection: close }; $exp-expect(4, [ '^Escape char' = sub { $exp-send($tosend); }], ); $exp-expect(4, [ '^HTTP/1.1' = sub { $data = $exp-match . $exp-after; }], ); $exp-soft_close; print --\n; print $data; __END__ On my system, the output from my local web-server appears. HTH -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: File locking issue
Nath, Alok (STSD) wrote: Hi, Hello, I am trying to save data in an xml file. What I am seeing is when I enable the file locking commands the file is completely wiped out.Just zero bytes. But it works correctly when I remove the file locking commands. Please help ?? What I am doing wrong . Regards, Alok use XML::Twig; use Fcntl qw(:DEFAULT :flock); my $registerFile = Register.xml ; my $twig = XML::Twig-new( pretty_print = 'indented' ); # Save data into the xml file my $node = XML::Twig::Elt-new( 'User', {'name' = $usr}, XML::Twig::Elt-new( 'email' = $email )) ; $node-paste( last_child = $twig-root ) ; #use lock to write into the file sysopen( FH, $registerFile, O_RDWR ) or die can't open $registerFile: $!; flock( FH, LOCK_EX) or die can't lock filename: $!; flock() locks the file, it is NOT changing the size. truncate(FH, 0) or die can't truncate filename: $!; truncate() changes the size of the file, here you are changing the size to zero bytes! $twig-parsefile( $registerFile ); $twig-print_to_file( $registerFile ) ; close(FH) John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: File locking issue
On 8/31/06, Nath, Alok (STSD) [EMAIL PROTECTED] wrote: What I am seeing is when I enable the file locking commands the file is completely wiped out.Just zero bytes. #use lock to write into the file sysopen( FH, $registerFile, O_RDWR ) or die can't open $registerFile: $!; flock( FH, LOCK_EX) or die can't lock filename: $!; truncate(FH, 0) or die can't truncate filename: $!; I think I know where the zero bytes are coming from. :-) $twig-parsefile( $registerFile ); What are you hoping to parse from the file that you've just truncated to zero bytes? Hope this helps! --Tom Phoenix Stonehenge Perl Training -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Totally lost - need a starting point
- Original Message - From: Mumia W. [EMAIL PROTECTED] To: Beginners List beginners@perl.org Sent: Thursday, August 31, 2006 11:29 AM Subject: [Bulk] Re: Totally lost - need a starting point On 08/31/2006 08:24 AM, Helen wrote: - Original Message - From: Charles K. Clarkson [EMAIL PROTECTED] To: beginners@perl.org Sent: Wednesday, August 30, 2006 10:16 PM Subject: [Bulk] RE: Totally lost - need a starting point Helen wrote: I am starting from scratch again reading the manual more completely. I am just running out of time on my deadline. Break down your task into small pieces and solve for those pieces. The first piece I can see is running an Expect script from a perl program. Do you know how to do that? I have no idea what Expect is, but searching for 'expect' and 'perl' in Google revealed a perl module for expect. The other pieces of your problem might include getting form values from an HTML form. CGI.pm can help with that. HTH, Charles K. Clarkson Yes I have a perl script running inside of the expect script, which works fine. I need to find a way to call the expect script and output the perl What perl? to the html page that is calling it. I found a version of expect for perl, but it didn't seem to be able to get the results that I was looking for, being able to telnet into a terminal and offer up passwords and such. then run another perl script that was picked as an option. Helen Hello Helen. First, you're confusing me; please don't top-post, and [Bulk] RE: should not have been added to the subject line; tame your mail-reader. Second, Expect.pm can do telnet: use strict; use warnings; use Expect; my $exp = Expect-spawn(telnet = qw(localhost 80)) or die (Spawn failed: $!\n); my $data = ''; my $tosend = q{GET / HTTP/1.1 Host: localhost Connection: close }; $exp-expect(4, [ '^Escape char' = sub { $exp-send($tosend); }], ); $exp-expect(4, [ '^HTTP/1.1' = sub { $data = $exp-match . $exp-after; }], ); $exp-soft_close; print --\n; print $data; __END__ On my system, the output from my local web-server appears. HTH -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response Thanks for the information and the routine for this group. I have written the expect scripts, which telnet into a terminal and then run scripts writen by the OEM. The scripts test the condition of the communication terminal. From all the researching that I have done in the past weeks, many people have asked this same question and the answer is never shown. I have been reading the tutorials and I am still not capable. Helen working example of an expect script called telenet_term_tu_status.exp #! /usr/bin/expect set terminal1 192.168.128.100 set username username set passwd password spawn telnet $terminal1 expect Username: send $username\r expect Password: send $passwd\r expect MSV- send tu_status\r expect MSV- send exit\r interact -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
what is wrong with my script
Hi guys, I have a CGI script to process some data from the visitors. The input data takes this format(row x column): 1 2 4 5 6 7 100 90 50 30 20 0 After processing I want to print out the result in the same format(row x column). But when I ran my script I only get a row only: 1 2 4 5 6 7 100 90 50 30 20 0 It looks like all lines are changed into a string only. Can anyone there give me a hand? Thanks, Li here is the code: #!c:/Perl/bin/perl.exe use warnings; use strict; use CGI qw/:standard/; ###create forms my $q=CGI-new(); print $q-header, $q-start_html('Survival Curve'), $q-h1('Survival Curve'), $q-start_form(), $q-textarea( -name='data', -default='', -rows=10, -columns=50), $q-p, $q-submit('Submit'), $q-end_form(); ##process the data my @data=(); my $cgi=CGI-new(); my $line=$cgi-param('data'); if ($line ne '' ){ my @temp=split/\t+/,$line; push @data,[EMAIL PROTECTED]; } for my $ref(@data){ print [EMAIL PROTECTED],\n; print $cgi-p; } print $q-end_html(); exit; __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: what is wrong with my script
On Thu, 2006-08-31 at 11:09 -0700, chen li wrote: Hi guys, I have a CGI script to process some data from the visitors. The input data takes this format(row x column): 1 2 4 5 6 7 100 90 50 30 20 0 After processing I want to print out the result in the same format(row x column). But when I ran my script I only get a row only: 1 2 4 5 6 7 100 90 50 30 20 0 It looks like all lines are changed into a string only. Can anyone there give me a hand? Browsers ignore most white space when rendering HTML. Since you're trying to present tabular data, you should put in inside a table. That should fix your formatting problems. HTH -- Joshua Colson [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
'use strict' and filehandles
Hi, I'm running into a Cach-22 with 'use strict', and filehandles. I have a program which opens two log files at the beginning: open(INFLOG,$info_log) || die Could not append to $info_log_file. Quitting.\n; open(ERRLOG,$error_log) || die Could not append to $error_log_file. Quitting.\n; Then throughout the program, different subroutines print to the log files, e.g. in code sample A below, ERRLOG and INFLOG are declared at the start. A subroutine tries to pass 'ERRLOG' or 'INFLOG' to the 'log' subroutine as a parameter. With 'strict refs' in use, this code generates this error: L:\test.pl Bareword ERRLOG not allowed while strict subs in use at L:\test.pl line 22. Execution of L:\test.pl aborted due to compilation errors. OK. I've tried a few things to work around this, but no matter what I've tried I just get an error that I can't do something with strict something-else (refs or subs) is in use. If I turn off strict it all just works, of course. Would someone please share how to solve this problem, while using the 'strict' directive? Thanks in advance, Ted CODE SAMPLE A START-- #!/usr/bin/perl use strict; use warnings; my $info_log = 'testinfo.log'; my $error_log = 'testerror.log'; open(INFLOG,$info_log) || die Could not append to $info_log. Quitting.\n; open(ERRLOG,$error_log) || die Could not append to $error_log. Quitting.\n; my $val = 1; something($val); $val=2; something_else($val); close ERRLOG; sub something { my ($val)[EMAIL PROTECTED]; my $bad = 1; my $good= 2; if ($val==$bad) { log(ERRLOG,Oh no! val $val is $bad, should be $good); } } sub something_else { my ($val)[EMAIL PROTECTED]; my $bad = 1; my $good= 2; if ($val != $bad) { log(INFLOG,Great! val $val is not $bad); } } sub log { my ($logfile,$msg)[EMAIL PROTECTED]; print $logfile $msg\n; } --CODE SAMPLE A END -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: 'use strict' and filehandles
If you want to pass a filehandle like that I would recommend using the FileHandle module. As your script stands, however, you have no reason to pass the filehandle to the subroutine. You can just do a print ERRLOG $scalar to print. -Original Message- From: Ted Fines [mailto:[EMAIL PROTECTED] Sent: Thursday, August 31, 2006 12:17 PM To: beginners@perl.org Subject: 'use strict' and filehandles Hi, I'm running into a Cach-22 with 'use strict', and filehandles. I have a program which opens two log files at the beginning: open(INFLOG,$info_log) || die Could not append to $info_log_file. Quitting.\n; open(ERRLOG,$error_log) || die Could not append to $error_log_file. Quitting.\n; Then throughout the program, different subroutines print to the log files, e.g. in code sample A below, ERRLOG and INFLOG are declared at the start. A subroutine tries to pass 'ERRLOG' or 'INFLOG' to the 'log' subroutine as a parameter. With 'strict refs' in use, this code generates this error: L:\test.pl Bareword ERRLOG not allowed while strict subs in use at L:\test.pl line 22. snip CODE SAMPLE A START-- #!/usr/bin/perl use strict; use warnings; my $info_log = 'testinfo.log'; my $error_log = 'testerror.log'; open(INFLOG,$info_log) || die Could not append to $info_log. Quitting.\n; open(ERRLOG,$error_log) || die Could not append to $error_log. Quitting.\n; # snip sub log { my ($logfile,$msg)[EMAIL PROTECTED]; print $logfile $msg\n; } --CODE SAMPLE A END -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Help with rotating logs
Hi, I am trying to write a script that can take log files in /var/log to a different directory (/opt/backup) every week. Each week it has to deposit the logs on a seperate directory with proper dates Can anyone has a script for this Thanks - Get your email and more, right on the new Yahoo.com
Re: 'use strict' and filehandles
Ted Fines wrote: Hi, Hello, I'm running into a Cach-22 with 'use strict', and filehandles. I have a program which opens two log files at the beginning: open(INFLOG,$info_log) || die Could not append to $info_log_file. Quitting.\n; open(ERRLOG,$error_log) || die Could not append to $error_log_file. Quitting.\n; Then throughout the program, different subroutines print to the log files, e.g. in code sample A below, ERRLOG and INFLOG are declared at the start. A subroutine tries to pass 'ERRLOG' or 'INFLOG' to the 'log' subroutine as a parameter. With 'strict refs' in use, this code generates this error: L:\test.pl Bareword ERRLOG not allowed while strict subs in use at L:\test.pl line 22. Execution of L:\test.pl aborted due to compilation errors. OK. I've tried a few things to work around this, but no matter what I've tried I just get an error that I can't do something with strict something-else (refs or subs) is in use. If I turn off strict it all just works, of course. Would someone please share how to solve this problem, while using the 'strict' directive? perldoc -q How do I pass filehandles between subroutines perldoc -q How can I use a filehandle indirectly John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: 'use strict' and filehandles
--- Original Message --- Ted Fines wrote: Hi, Hello, I'm running into a Cach-22 with 'use strict', and filehandles. I have a program which opens two log files at the beginning: open(INFLOG,$info_log) || die Could not append to $info_log_file. Quitting.\n; open(ERRLOG,$error_log) || die Could not append to $error_log_file. Quitting.\n; Then throughout the program, different subroutines print to the log files, e.g. in code sample A below, ERRLOG and INFLOG are declared at the start. A subroutine tries to pass 'ERRLOG' or 'INFLOG' to the 'log' subroutine as a parameter. With 'strict refs' in use, this code generates this error: L:\test.pl Bareword ERRLOG not allowed while strict subs in use at L:\test.pl line 22. Execution of L:\test.pl aborted due to compilation errors. OK. I've tried a few things to work around this, but no matter what I've tried I just get an error that I can't do something with strict something-else (refs or subs) is in use. If I turn off strict it all just works, of course. Would someone please share how to solve this problem, while using the 'strict' directive? perldoc -q How do I pass filehandles between subroutines Perfect. Perfect. Perfect. Thank you! Ted -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Trouble with variable scoping
Roman Daszczyszak wrote: In my perl script, I have a global variable called @excludedIPAddresses, declared at the top of the script using my: [...] When I run this, I get an error Can't localize lexical variable. I understand that it's because the variable is declared using my; what I don't understand is why, or what I should declare the variable as, since if I leave out my I get an error using use strict. The Cmy and Cour declarators have less to do with scope than with storage and access. Variables declared with Cmy are limited to the scope in which they are declared and any inner scopes, and are known as local or lexical variables. They may be re-declared within an inner scope, in which case the inner scope masks the outer, but the outer variable still exists, it retains its value, and it will be accessible again when control returns to the outer scope. For Cmy variables, storage is tied to the scope in which the variable is declared, in a scratchpad that is thrown away at the end of the scope. Cmy variables can also be global variables when declared in a large scope. Variables declared with Cour are tied to a package and are stored as part of the package, and are known as package variables. They are accessible anywhere in the package by using the short name or outside the package by using the fully qualified name (i.e. type-glyphpackage-name::variable-name). You can simulate the scoping rules of Cmy variables with the Clocal function(?). Clocal creates a copy of the _value_ of the outer package variable that masks any previous value. The new _value_ exists only within the scope in which the Clocal function was used. Clocal does not declare a variable unless you run without the 'strict' pragma. Without 'strict' it creates the package variable for you, but when using 'strict', it generates an error requiring you to either declare it with Cour or to fully qualify it with the package name. Mixing variables of the same name with different storage is where things are sometimes less clear. (And some of the rules have changed slightly through different revisions of perl.) So, generally you should avoid using variables of the same name with different storage declarators. When Perl sees code like: use strict; my $foo; local $foo; It first creates the lexical variable $foo. It then evaluates the call to Clocal by first trying to resolve the name of the variable it refers to. Since the only $foo it knows about is a lexical variable, it warns you that you are trying to use Clocal on a lexical variable. If you declare a package variable: use strict; my $foo; our $foo; local $foo; Perl will resolve the name $foo referenced in the call to Clocal to the package variable and will happily use it. IOW, the error comes about during the process of trying to resolve the variable name. If the variable name resolves to the wrong type of variable, you get an error. You could also help Perl to resolve the name by fully qualifying it: use strict; my $foo; local $PackageName::foo; __END__ Regards, Randy. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Trouble with variable scoping
Randy W. Sims wrote: Roman Daszczyszak wrote: In my perl script, I have a global variable called @excludedIPAddresses, declared at the top of the script using my: [...] When I run this, I get an error Can't localize lexical variable. I understand that it's because the variable is declared using my; what I don't understand is why, or what I should declare the variable as, since if I leave out my I get an error using use strict. The Cmy and Cour declarators have less to do with scope than with storage and access. Variables declared with Cmy are limited to the scope in which they are declared and any inner scopes, and are known as local or lexical variables. They may be re-declared within an inner scope, in which case the inner scope masks the outer, but the outer variable still exists, it retains its value, and it will be accessible again when control returns to the outer scope. For Cmy variables, storage is tied to the scope in which the variable is declared, in a scratchpad that is thrown away at the end of the scope. Cmy variables can also be global variables when declared in a large scope. Variables declared with Cour are tied to a package and are stored as part of the package, and are known as package variables. They are accessible anywhere in the package by using the short name or outside the package by using the fully qualified name (i.e. type-glyphpackage-name::variable-name). Variables declared with our() have the same scoping rules as variables declared with my(). $ perl -le' use warnings; use strict; package me; our $x = q/our/; my $y = q/my/; package main; print for $x, $y; ' our my John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Trouble with variable scoping
John W. Krahn wrote: Randy W. Sims wrote: Roman Daszczyszak wrote: In my perl script, I have a global variable called @excludedIPAddresses, declared at the top of the script using my: [...] When I run this, I get an error Can't localize lexical variable. I understand that it's because the variable is declared using my; what I don't understand is why, or what I should declare the variable as, since if I leave out my I get an error using use strict. The Cmy and Cour declarators have less to do with scope than with storage and access. Variables declared with Cmy are limited to the scope in which they are declared and any inner scopes, and are known as local or lexical variables. They may be re-declared within an inner scope, in which case the inner scope masks the outer, but the outer variable still exists, it retains its value, and it will be accessible again when control returns to the outer scope. For Cmy variables, storage is tied to the scope in which the variable is declared, in a scratchpad that is thrown away at the end of the scope. Cmy variables can also be global variables when declared in a large scope. Variables declared with Cour are tied to a package and are stored as part of the package, and are known as package variables. They are accessible anywhere in the package by using the short name or outside the package by using the fully qualified name (i.e. type-glyphpackage-name::variable-name). Variables declared with our() have the same scoping rules as variables declared with my(). $ perl -le' use warnings; use strict; package me; our $x = q/our/; my $y = q/my/; package main; print for $x, $y; ' our my Sort of. This is one of Perl's weird features. I'm not quite sure how to define the behavior... The Cmy variable is scoped from the point of declaration to the end of the enclosing scope, which, in this case, is file scope. This is not unusual. The Cour declaration creates the package variable $me::x. If you add to the end of your example: use Data::Dumper; print Dumper \%me::; You'll notice that the symbol 'x' has been created as an alias for *me::x. So with Cour we are still talking about a package variable. What I'm unsure of is how the alias is set up. There is no symbol 'x' in main: print Dumper \%:: Both $x and $y are only valid from the point they are declared until the end of the file. If package 'me' were defined in a different file, both $x and $y would not be accessible from package 'main'. So I guess I don't really know technically how $x is resolved to $me::x from within main. Maybe it's quietly added to the lexical scratchpad for the file scope as an alias to the package symbol of the same name? If that is the case, then Cour and Cmy would always share the same scoping, but have different storage and access semantics. Randy. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: figuring out if a number/character string is null in Perl
On 8/30/06, Mary Anderson [EMAIL PROTECTED] wrote: Hi All, I know this isn't strictly a cgi problem, but it is arising in a cgi application. I have a loop which reads certain fields, hashed on names. Some of my fields hold character strings, some hold numbers. Sometimes the number field is a blank. I need a test on the field value $fieldValue which will tell me if my field was blank regardless of whether it holds a character string or a number. I would like to say something like $fieldValue = (($fieldValue == 0) or $fieldValue) ? $fieldValue : 'null' but perl appears to have a strange interpretation of $fieldValue == 0 if $fieldValue is a character. Thanks Mary I have seen references to a function which will do the trick, but it is not mentioned in the camel book. Well, fisrt of all, you need to decide what you mean by null, and blank. In most cases they aren't the same thing. Null normally means a string containing the value '\0'. This is especially true if you are interacting with anything that has C code in it. Perl programmers, for the most part, are interested in whether a particular value is defined, in which case you can use the defined() function, e.g.: next unless defined($var); When working with hashes, exists() is also useful, perldoc -f defined, perldoc -f exists, and perldoc -f undef for more info. in many contexts, a simple test for boolean truth suffices as well, e.g.: next unless $var; But the catch there is that '0', '', and undef will all fail, so you can get into trouble if '0' is a legal value. Finally, none of these will catch a blank, that is, a sting composed entirely of whitespace. to handle that, you need a regex, something like next if $var =~ /^\s*$/; HTH, -- jay -- This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] private and confidential daggerquill [at] gmail [dot] com http://www.tuaw.com http://www.downloadsquad.com http://www.engatiki.org values of β will give rise to dom!