using 'my' problem

2004-10-27 Thread Murphy, Ged (Bolton)
In order to use the array @files in the below code outside of the
subroutine, so I need to declare @files globally? 
I'm getting errors at the moment: Global symbol "@files" requires explicit
package name

If so, where is the correct place to declare them in terms of neatness? At
the start of the program? above the subroutine?

Thanks.


#!/usr/bin/perl

use strict;
use warnings;
#use Image::Magick::Thumbnail;

# set directory to read images from
my $dir = "/ged/code/photo/vx";

# read all files from given directory
opendir DH, $dir or die "Cannot open $dir: $!";
foreach my $file (readdir DH) {
   unless ($file =~ /^\./) {
  push my(@files), $file;
   }
}
closedir DH;

#check files are in the array
print $_ foreach @files;






The information contained in this message or any of its
attachments is confidential and is intended for the exclusive
use of the addressee. The information may also be legally
privileged. The views expressed may not be company policy,
but the personal views of the originator. If you are not the
addressee, any disclosure, reproduction, distribution or other
dissemination or use of this communication is strictly prohibited.
If you have received this message in error, please contact
[EMAIL PROTECTED] 
 and then delete this message. 

Exide Technologies is an industrial and transportation battery
producer and recycler with operations in 89 countries.
Further information can be found at www.exide.com



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: using 'my' problem

2004-10-27 Thread Thomas Bätzler
Murphy, Ged (Bolton) asked:
> In order to use the array @files in the below code outside of 
> the subroutine, so I need to declare @files globally?
> I'm getting errors at the moment: Global symbol "@files" 
> requires explicit package name
> 
> If so, where is the correct place to declare them in terms of 
> neatness? At the start of the program? above the subroutine?

That depends on your programming style. Right above the loop
would do.

Personally, I like to declare my globals in a special section
at the top of my program - sometimes even with a short comment
as to what they're supposed to hold. I find this helps me when
I look at the code much later.

YMMV.

> The information contained in this message or any of its 
> attachments is confidential and is intended for the exclusive 
[...]

Blah. Could you please get rid of that when posting to the list?

Thomas

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: using 'my' problem

2004-10-27 Thread JupiterHost.Net

Murphy, Ged (Bolton) wrote:
In order to use the array @files in the below code outside of the
subroutine, so I need to declare @files globally? 
I'm getting errors at the moment: Global symbol "@files" requires explicit
package name

If so, where is the correct place to declare them in terms of neatness? At
the start of the program? above the subroutine?
I don't see a subroutine, but I do see a loop ;p,
I'd say reight before opendir()
 my @files = ();
HTH :)
Lee.M - JupiterHost.Net
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: using 'my' problem

2004-10-27 Thread Gunnar Hjalmarsson
JupiterHost.Net wrote:
I'd say reight before opendir()
 my @files = ();
Which is equivalent to
my @files;
since my() has the side-effect of clearing the declared variable.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: using 'my' problem

2004-10-27 Thread JupiterHost.Net

Gunnar Hjalmarsson wrote:
JupiterHost.Net wrote:
I'd say reight before opendir()
 my @files = ();

Which is equivalent to
my @files;
since my() has the side-effect of clearing the declared variable.
Except = (); will help avoid "uninitialized value" warnings no?
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: using 'my' problem

2004-10-27 Thread Gunnar Hjalmarsson
JupiterHost.Net wrote:
Gunnar Hjalmarsson wrote:
JupiterHost.Net wrote:
 my @files = ();
Which is equivalent to
my @files;
since my() has the side-effect of clearing the declared variable.
Except = (); will help avoid "uninitialized value" warnings no?
How?
use warnings;
my @files = ();
print $files[0];
Result:
Use of uninitialized value in print at ...
You may be mixing it up with assigning the null string to a scalar variable:
my $scalar = '';
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: using 'my' problem

2004-10-27 Thread JupiterHost.Net

JupiterHost.Net wrote:

Gunnar Hjalmarsson wrote:
JupiterHost.Net wrote:
I'd say reight before opendir()
 my @files = ();

Which is equivalent to
my @files;
since my() has the side-effect of clearing the declared variable.

Except = (); will help avoid "uninitialized value" warnings no?
That would be a "no"
That is only for scalars which makes so much sense now :)
$ perl -mstrict -we 'my $files;print $files;'
Use of uninitialized value in print at -e line 1.
$ perl -mstrict -we 'my @files;print $_ for @files;'
$ perl -mstrict -we 'my %files;print $_ for keys %files'
$
No more
 my @this = ();
or
 my %that = ();
for me!
So scratch what I said, what Gunnar said is better.
my @files;
is what you want, as my @files = (); is basically pointless ;p
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: using 'my' problem

2004-10-27 Thread Gunnar Hjalmarsson
JupiterHost.Net wrote:
So scratch what I said, what Gunnar said is better.
my @files;
is what you want, as my @files = (); is basically pointless ;p
Now I think you are too hard to yourself. :)  I've noticed that some 
prefer @files = () for clarity, so it may not be totally pointless.

At the same time it's important to be aware of that side-effect of my(), 
or else you may encounter surprises...

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: using 'my' problem

2004-10-27 Thread Gunnar Hjalmarsson
Gunnar Hjalmarsson wrote:
I've noticed that some prefer @files = () for clarity, ...
Sorry, should have been
I've noticed that some prefer my @files = () for clarity, ...
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: using 'my' problem

2004-10-27 Thread John W. Krahn
Murphy, Ged (Bolton) wrote:
In order to use the array @files in the below code outside of the
subroutine, so I need to declare @files globally? 
You could but it is not really necessary.
I'm getting errors at the moment: Global symbol "@files" requires explicit
package name
That is because you have to declare it somewhere.  :-)
If so, where is the correct place to declare them in terms of neatness? At
the start of the program? above the subroutine?
It is usually best to use the smallest possible scope.

#!/usr/bin/perl
use strict;
use warnings;
#use Image::Magick::Thumbnail;
# set directory to read images from
my $dir = "/ged/code/photo/vx";
# read all files from given directory
opendir DH, $dir or die "Cannot open $dir: $!";
foreach my $file (readdir DH) {
   unless ($file =~ /^\./) {
  push my(@files), $file;
   }
}
For your simple example I would do this:
my @files = grep !/^\./, readdir DH;
Or perhaps:
my @files = grep /^[^.]/, readdir DH;

closedir DH;
#check files are in the array
print $_ foreach @files;

John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: using 'my' problem

2004-10-31 Thread Zeus Odin
The reason why you don't get the uninitialized warning in the 2nd and 3rd
examples below is that your print is within the for loop. Since both @files
and keys %files contain nothing, the innards of the for loop NEVER get
executed. Therefore, the print is not attempted at all for examples 2 and 3.

-ZO

"JupiterHost.Net" <[EMAIL PROTECTED]> wrote in message ...
>
> $ perl -mstrict -we 'my $files;print $files;'
> Use of uninitialized value in print at -e line 1.
> $ perl -mstrict -we 'my @files;print $_ for @files;'
> $ perl -mstrict -we 'my %files;print $_ for keys %files'
> $



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: using 'my' problem

2004-11-03 Thread JupiterHost.Net

Zeus Odin wrote:
The reason why you don't get the uninitialized warning in the 2nd and 3rd
examples below is that your print is within the for loop. Since both @files
and keys %files contain nothing, the innards of the for loop NEVER get
executed. Therefore, the print is not attempted at all for examples 2 and 3.
Correct, except I still think @ ans % don't get uninitialized value 
warnings while $ does:

$perl -mstrict -we 'my $foo;print "hi\n" for $foo;'
hi
$
That shows what you said to be accurate.
Then no error:
$ perl -mstrict -we 'my @foo;print @foo;'
$
$ perl -mstrict -we 'my %foo;print %foo;'
$
Error:
$ perl -mstrict -we 'my $foo;print $foo;'
Use of uninitialized value in print at -e line 1.
$
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: using 'my' problem

2004-11-03 Thread Zeus Odin
To see the code that is actually getting compiled, try:

$ perl -MO=Deparse -mstrict -we 'my @foo;print @foo;'


"JupiterHost.Net" <[EMAIL PROTECTED]> wrote in message ...

> Correct, except I still think @ ans % don't get uninitialized value
> warnings while $ does:
>
> $perl -mstrict -we 'my $foo;print "hi\n" for $foo;'
> hi
> $
>
> That shows what you said to be accurate.
>
> Then no error:
> $ perl -mstrict -we 'my @foo;print @foo;'
> $
> $ perl -mstrict -we 'my %foo;print %foo;'
> $
>
> Error:
> $ perl -mstrict -we 'my $foo;print $foo;'
> Use of uninitialized value in print at -e line 1.
> $



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: using 'my' problem

2004-11-03 Thread JupiterHost.Net

Zeus Odin wrote:
To see the code that is actually getting compiled, try:
$ perl -MO=Deparse -mstrict -we 'my @foo;print @foo;'
Ok, not sure what that has to do with @ and % not getting uninitialized 
warnings and $ getting them...

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: using 'my' problem

2004-11-06 Thread Zeus Odin
Apologies, I was asleep at the wheel during my last response.
:-)

The only answer I have is that this is how the designers of Perl decided
that the printing of undefined $, @, and % would work. This is probably due
to the fact that the undefined value of a scalar *is* different from the
undefined value of a hash or an array. Take for instance:

$ perl -mstrict -MData::Dumper -we "my $; print Dumper $s;"
$VAR1 = undef;

$ perl -mstrict -MData::Dumper -we "my @a; print Dumper [EMAIL PROTECTED];"
$VAR1 = [];

$ perl -mstrict -MData::Dumper -we "my %h; print Dumper %h;"

$ perl -mstrict -MData::Dumper -we "my %h; print keys %h;"

$ perl -mstrict -MData::Dumper -we "my %h; print Dumper \(keys %h);"

$ perl -e "print ()"

$ perl -e "print +()"

$ perl -mstrict -we "print undef;"
Use of uninitialized value in print at -e line 1.

$ perl -mstrict -we "print (undef);"
Use of uninitialized value in print at -e line 1.

$ perl -mstrict -we "print +(undef);"
Use of uninitialized value in print at -e line 1.


This says to me that printing undef (unitilialized scalar) is different from
printing an empty list (undefined array). However, printing an undefined
hash in any fashion, outputs nothing. I did not expect that.

Hope that helps,
ZO



"JupiterHost.Net" <[EMAIL PROTECTED]> wrote in message ...
>
> Zeus Odin wrote:
> > To see the code that is actually getting compiled, try:
> >
> > $ perl -MO=Deparse -mstrict -we 'my @foo;print @foo;'
>
> Ok, not sure what that has to do with @ and % not getting uninitialized
> warnings and $ getting them...



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: using 'my' problem

2004-11-06 Thread Zeus Odin
Two small mistakes:

"Zeus Odin" <[EMAIL PROTECTED]> wrote in message ...

> $ perl -mstrict -MData::Dumper -we "my $; print Dumper $s;"
^^^
 $s
> $VAR1 = undef;

> $ perl -mstrict -MData::Dumper -we "my %h; print Dumper %h;"
  ^^^
  \%h
$VAR1 = {};

Therefore, the hash, like the array, both resolve to an empty list not
undef.

-ZO



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: using 'my' problem

2004-11-06 Thread JupiterHost.Net

Zeus Odin wrote:
Apologies, I was asleep at the wheel during my last response.
:-)
The only answer I have is that this is how the designers of Perl decided
that the printing of undefined $, @, and % would work. This is probably
due
to the fact that the undefined value of a scalar *is* different from the
undefined value of a hash or an array. Take for instance:
Correct, I had mearly stated I hadn't realized that @arr; and %hsh; 
woudn't give uninitialized value warnings until Gunnar I beleive 
poiinted it out to me :)

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: using 'my' problem

2004-11-06 Thread John W. Krahn
Zeus Odin wrote:
Apologies, I was asleep at the wheel during my last response.
:-)
The only answer I have is that this is how the designers of Perl decided
that the printing of undefined $, @, and % would work. This is probably due
to the fact that the undefined value of a scalar *is* different from the
undefined value of a hash or an array. Take for instance:
A hash or an array don't have an undefined value.  They can either contain 
something or be empty.

perldoc -f defined
[snip]
   Use of "defined" on aggregates (hashes and arrays) is deprecated.  It
   used to report whether memory for that aggregate has ever been
   located.  This behavior may disappear in future versions of Perl.
   You should instead use a simple test for size:
perldoc perldata
[snip]
   There are actually two varieties of null strings (sometimes referred to
   as "empty" strings), a defined one and an undefined one.  The defined
   version is just a string of length zero, such as "".  The undefined
   version is the value that indicates that there is no real value for
   something, such as when there was an error, or at end of file, or when
   you refer to an uninitialized variable or element of an array or hash.
   Although in early versions of Perl, an undefined scalar could become
   defined when first used in a place expecting a defined value, this no
   longer happens except for rare cases of autovivification as explained
   in perlref.  You can use the defined() operator to determine whether a
   scalar value is defined (this has no meaning on arrays or hashes), and
   the undef() operator to produce an undefined value.
perldoc perltrap
[snip]
 * (Hashes)
  Hashes get defined before use
  local($s,@a,%h);
  die "scalar \$s defined" if defined($s);
  die "array [EMAIL PROTECTED] defined" if defined(@a);
  die "hash \%h defined" if defined(%h);
  # perl4 prints:
  # perl5 dies: hash %h defined
  Perl will now generate a warning when it sees defined(@a) and
  defined(%h).
In scalar context a hash or array will return a numeric value:
$ perl -le'my %x; print scalar %x'
0
$ perl -le'my @x; print scalar @x'
0
$ perl -le'my %x = qw/a b/; print scalar %x'
1/8
$ perl -le'my @x = qw/a b/; print scalar @x'
2
In list context a hash or array (or list) will return their contents.
$ perl -le'my @x = qw/a b/; print @x'
ab
$ perl -le'my %x = qw/a b/; print %x'
ab
$ perl -le'print qw/a b/'
ab

John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]