Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Pls help old perl-monger with new version syntax?

Reply
Thread Tools

Pls help old perl-monger with new version syntax?

 
 
r.mariotti@fdcx.net
Guest
Posts: n/a
 
      07-24-2012
Been doing perl since mid-90's. Just upgraded to the latest version
5.14.2 and many of my perl scripts are thowing errors like no
tomorrow.

I've spent several days trying to resolve the errors without much
success so I thought I would post here as many must have experienced
the same.

I DID research quite extensively with perldoc and google and tried
literally all the suggestions but here it goes anyway.

It appears that using the pragmas warning and strict the newest
version is requiring predefinition of variables prior to their use.

Here's a snippet of my code:

# Define pragmas
use warnings;
use strict;
# Define ALL needed variables & indecies
my $fn = '' unless defined($fn);
my $ln = '' unless defined($ln);
my $opt = '' unless defined($opt);
my $rr = 0 unless defined($rr);
my $x1 = '' unless defined($x1);
my $x2 = '' unless defined($x2);
my $x3 = '' unless defined($x3);
my $x4 = '' unless defined($x4);

Pls note that the '' chars above are two single quotes indicating a
NULL value just to initialize the variables.

The infamous error is

Global symbol "$fn" requires explicit package name

Oh the joy. Anyone care to contribute as it will be very, very
appreciated.

Thanks all
 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      07-24-2012
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

[...]


> # Define ALL needed variables & indecies
> my $fn = '' unless defined($fn);


[...]

> Global symbol "$fn" requires explicit package name


This refers to the $fn used as argument for defined. That's the '$fn'
in the symbol table of the current package, not the my-variable.
 
Reply With Quote
 
 
 
 
Peter J. Holzer
Guest
Posts: n/a
 
      07-24-2012
On 2012-07-24 12:10, (E-Mail Removed) <(E-Mail Removed)> wrote:
> Been doing perl since mid-90's. Just upgraded to the latest version
> 5.14.2 and many of my perl scripts are thowing errors like no
> tomorrow.


What was the last version of perl you used? Your script doesn't work
with perl 5.8.8 (2006), so it probably doesn't work since at least 5.8.0
(2002).


> I've spent several days trying to resolve the errors without much
> success so I thought I would post here as many must have experienced
> the same.
>
> I DID research quite extensively with perldoc and google and tried
> literally all the suggestions but here it goes anyway.
>
> It appears that using the pragmas warning and strict the newest
> version is requiring predefinition of variables prior to their use.
>
> Here's a snippet of my code:
>
> # Define pragmas
> use warnings;
> use strict;
> # Define ALL needed variables & indecies
> my $fn = '' unless defined($fn);


What are you trying to do here?

defined() doesn't check if a variable exists, it checks whether the
value is (not) undef.

So this means:

if the value of the variable $fn is undef
then
create a new variable $fn in this lexical scope
and assign the empty string to it.

Except that the variable is created at compile time, so it will
always exist in this scope even if there happened to be a variable $fn
with a defined value in an outer scope.

(There is a similar but not quite identical construct for state
variables pre 5.10 - for these, use the new state feature, but I doubt
that this is what you wanted)

I think you just meant

my $fn = '';

hp


--
_ | Peter J. Holzer | Deprecating human carelessness and
|_|_) | Sysadmin WSR | ignorance has no successful track record.
| | | (E-Mail Removed) |
__/ | http://www.hjp.at/ | -- Bill Code on (E-Mail Removed)
 
Reply With Quote
 
Justin C
Guest
Posts: n/a
 
      07-24-2012
On 2012-07-24, (E-Mail Removed) <(E-Mail Removed)> wrote:
> Been doing perl since mid-90's. Just upgraded to the latest version
> 5.14.2 and many of my perl scripts are thowing errors like no
> tomorrow.
>
> I've spent several days trying to resolve the errors without much
> success so I thought I would post here as many must have experienced
> the same.
>
> I DID research quite extensively with perldoc and google and tried
> literally all the suggestions but here it goes anyway.
>
> It appears that using the pragmas warning and strict the newest
> version is requiring predefinition of variables prior to their use.
>
> Here's a snippet of my code:
>
> # Define pragmas
> use warnings;
> use strict;
> # Define ALL needed variables & indecies
> my $fn = '' unless defined($fn);
> my $ln = '' unless defined($ln);
> my $opt = '' unless defined($opt);
> my $rr = 0 unless defined($rr);
> my $x1 = '' unless defined($x1);
> my $x2 = '' unless defined($x2);
> my $x3 = '' unless defined($x3);
> my $x4 = '' unless defined($x4);


In addition to what others have said, I think this is
standard practice these days:

my ($fh, $ln, $opt, $x1, $x2, $x3, $x4);
my $rr = 0;

(though it's definitely preferred to declare them
only within the scope of where they're used)


It may be possible to include the declration of $rr
in the first line, but it depends on how it's used.
If it's just a counter, AIUI initialising with: my
$rr; and then later doing: $rr++; would be enough to
make it '1'.

I'm guessing that $x1, $x2, $x3 and $x4 are in some
way related. It may be better to have them in an
array or hash:

my @x;
push @x, where_x1_comes_from();
push @x, where_x2_comes_from();
etc.

or
my %x;
$x{one} = where_x1_comes_from();
$x{two} = where_x2_comes_from()l

....then later:

my $where_i'd_use_x1 = $x[0];
where_i'd_use_x2( $x[1] );

or

my $where_i'd_use_x1 = $x{one};
where_i'd_ise_x2($x{two});

I'm far from good at this stuff, stick around and
watch the experts tear holes in in the above, we'll
both learn something!

Justin.

--
Justin C, by the sea.
 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      07-24-2012
On 2012-07-24 14:17, Justin C <(E-Mail Removed)> wrote:
> On 2012-07-24, (E-Mail Removed) <(E-Mail Removed)> wrote:
>> # Define pragmas
>> use warnings;
>> use strict;
>> # Define ALL needed variables & indecies
>> my $fn = '' unless defined($fn);
>> my $ln = '' unless defined($ln);
>> my $opt = '' unless defined($opt);
>> my $rr = 0 unless defined($rr);
>> my $x1 = '' unless defined($x1);
>> my $x2 = '' unless defined($x2);
>> my $x3 = '' unless defined($x3);
>> my $x4 = '' unless defined($x4);

>
> In addition to what others have said, I think this is
> standard practice these days:
>
> my ($fh, $ln, $opt, $x1, $x2, $x3, $x4);
> my $rr = 0;


Why do you initialize only $rr and not the other variables?


> It may be possible to include the declration of $rr
> in the first line, but it depends on how it's used.


It also depends on how $fn .. $x4 are used. An empty string is not the
same as undef. If it is important that $fn .. $x4 contain an empty
string, they must be initialized to the empty string. If they can (or
even should) contain undef, then they need not be initialized
explicitely (perl will automatically initialize them to undef). Same for
$rr: If it is important that it contains the number 0, it must be
initialized. If it can contain undef, this is not necessary.

hp

--
_ | Peter J. Holzer | Deprecating human carelessness and
|_|_) | Sysadmin WSR | ignorance has no successful track record.
| | | (E-Mail Removed) |
__/ | http://www.hjp.at/ | -- Bill Code on (E-Mail Removed)
 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      07-24-2012
Ben Morrow <(E-Mail Removed)> writes:
> Quoth Justin C <(E-Mail Removed)>:


[...]

>> (though it's definitely preferred to declare them
>> only within the scope of where they're used)

>
> Yes. In general, don't create a variable until you have something useful
> to put in it. (This isn't always possible.)


In general, don't declare variables in a lexical scope unless it
happens to be the outmost lexical scope of a subroutine. As soon as
this starts to become unwieldly, structure the code into a set of
subroutines and a controlling superroutine.

 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      07-24-2012
On 2012-07-24 15:41, Ben Morrow <(E-Mail Removed)> wrote:
> Quoth "Peter J. Holzer" <(E-Mail Removed)>:
>> On 2012-07-24 12:10, (E-Mail Removed) <(E-Mail Removed)> wrote:
>> > my $fn = '' unless defined($fn);

><snip>
>>
>> So this means:
>>
>> if the value of the variable $fn is undef
>> then
>> create a new variable $fn in this lexical scope
>> and assign the empty string to it.
>>
>> Except that the variable is created at compile time, so it will
>> always exist in this scope even if there happened to be a variable $fn
>> with a defined value in an outer scope.

>
> That isn't quite true. The RHS of the 'unless' is logically before the
> start of the scope of the new $fn, so this will indeed test the
> definedness of $fn in the outer scope.


Yes, that's what I wrote.

But test will not determine whether a new $fn at the inner scope will be
created, just whether it will be initialized.

Consider:

#!/usr/bin/perl
use warnings;
use strict;
use 5.010;

{
my $fn = 5; # 7

{
my $fn = 3 unless $fn; # 10

say "inner scope: ", $fn; # 12
} # 13
say "outer scope: ", $fn; # 14

} # 16
__END__

This prints:

Use of uninitialized value $fn in say at ./foo line 12.
inner scope:
outer scope: 5

At compile time the compiler sees a my $fn declaration at line 7 and one
at line 10 in nested scopes. So it creates two $fn variables: One with a
scope from line 7 to line 16 and one from line 10 to 13.

It also creates code that assigns 3 to the inner $fn only if the outer
$fn is not true.

So at run time, the interpreter notices that the outer $fn has a true
value (5) and skips the assignment of 3 to the inner $fn. However the
inner $fn still exists with an initial value of undef, und the say at
line 12 will attempt to print that value (which results in a warning).

This is the basis for the old "my $x if 0" hack (but you you know that).

hp


--
_ | Peter J. Holzer | Deprecating human carelessness and
|_|_) | Sysadmin WSR | ignorance has no successful track record.
| | | (E-Mail Removed) |
__/ | http://www.hjp.at/ | -- Bill Code on (E-Mail Removed)
 
Reply With Quote
 
r.mariotti@fdcx.net
Guest
Posts: n/a
 
      07-25-2012
Gents,

Thanks for all the good comments. Makes the brain churn even harder.

Here's the skinny on why I asked this:

Again, in most of the perl programs I've created over the years, I
normally have a section near the top of the program where I define the
necessary variables and/or arrays/hashes/etc. In most cases these
fields will be used in the logic that follows and set to the
appropriate value when used. It could be a pointer, a temp variable,
the target of a function, etc.

Not until I began working with this version (5.14.2) included with
Ubuntu 12.04 LTS did I start receiving the referenced error message.
My prior version is what came with Ubuntu 8.04 so I can't say exactly
which one that was. But my existing perl programs did NOT throw that
error back then. Only when I try running them on this new version did
I start seeing this error.

The snippet of code I included in my post was an attempt to come up
with a process that would initialize these fields if they were not
aleady defined. That's why I used the unless defined clause.

My understanding of this function was that it was the perl equivalent
of the php isset function. However, even when I remove the unless
portion of the statements I still receive the same use of
uninitialized variable blah blah.

All I'm trying to do is understand how I can satisfy the syntax so the
programs resume working. They were in production.

So please keep up the comments as I and I'm sure others find them
informative. And if you understand my issue more clearly perhaps more
suggestions will follow.

Thanks
 
Reply With Quote
 
Xho Jingleheimerschmidt
Guest
Posts: n/a
 
      07-25-2012
On 07/24/2012 02:34 PM, Ben Morrow wrote:
>
> Quoth Eli the Bearded <*@eli.users.panix.com>:
>>
>> Panix has 5.00403 installed.

>
> If that's the only version of perl they provide then find somewhere
> else. Versions of perl before 5.8.1 have a known security hole, apart
> from anything else (the hash algorithmic complexity attack).


That seems like a vanishingly small use-case as a security hole. It is
mostly a "Want to deny service to your own bad self? Well, go right
ahead, dumb-ass" hole.

The people who are using the vanishingly small use-case, of course,
would be expected to care.

Xho
 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      07-25-2012
On 2012-07-25 02:51, (E-Mail Removed) <(E-Mail Removed)> wrote:
> Not until I began working with this version (5.14.2) included with
> Ubuntu 12.04 LTS did I start receiving the referenced error message.
> My prior version is what came with Ubuntu 8.04 so I can't say exactly
> which one that was.


Probably 5.8.8 or 5.10.0. The code you posted definitely wouldn't have
worked with those versions.

> But my existing perl programs did NOT throw that
> error back then. Only when I try running them on this new version did
> I start seeing this error.
>
> The snippet of code I included in my post was an attempt to come up
> with a process that would initialize these fields if they were not
> aleady defined. That's why I used the unless defined clause.


So this was already an attempt to work around the error message?

Please post a script which did work on Ubuntu 8.04 and which doesn't
work on Ubuntu 12.04.


> My understanding of this function was that it was the perl equivalent
> of the php isset function.


I don't understand PHP well enough to answer that question. The perl
defined() function is simple: It returns false if its argument is undef,
otherwise true.

> However, even when I remove the unless
> portion of the statements I still receive the same use of
> uninitialized variable blah blah.


So, this script:

# Define pragmas
use warnings;
use strict;
# Define ALL needed variables & indecies
my $fn = '' ;
my $ln = '' ;
my $opt = '' ;
my $rr = 0 ;
my $x1 = '' ;
my $x2 = '' ;
my $x3 = '' ;
my $x4 = '' ;

produces a "use of uninitialized variable" error? I find that hard to
believe.

hp


--
_ | Peter J. Holzer | Deprecating human carelessness and
|_|_) | Sysadmin WSR | ignorance has no successful track record.
| | | (E-Mail Removed) |
__/ | http://www.hjp.at/ | -- Bill Code on (E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
URGENT - Pls help...pls recommend - laptop purchase irfansmith@gmail.com Computer Information 2 08-15-2008 11:34 PM
Re: Where to get stand alone Dot Net Framework version 1.1, version2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? MowGreen [MVP] ASP .Net 5 02-09-2008 01:55 AM
Re: Where to get stand alone Dot Net Framework version 1.1, version 2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? PA Bear [MS MVP] ASP .Net 0 02-05-2008 03:28 AM
Re: Where to get stand alone Dot Net Framework version 1.1, version 2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? V Green ASP .Net 0 02-05-2008 02:45 AM
pls, help.. i need a number..pls olabanji timothy MCSE 7 09-10-2003 04:02 PM



Advertisments