[19068] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 1263 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Jul 7 14:11:29 2001

Date: Sat, 7 Jul 2001 11:10:12 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <994529412-v10-i1263@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Sat, 7 Jul 2001     Volume: 10 Number: 1263

Today's topics:
        Use of uninitialized value in FORMAT?? <miscellaneousemail@yahoo.com>
    Re: Use of uninitialized value in FORMAT?? (Tad McClellan)
    Re: Use of uninitialized value in FORMAT?? <bwalton@rochester.rr.com>
    Re: Using a hash passed by reference inside a function? (Mark Jason Dominus)
    Re: Using the hash DB_File uses, after untie it?? (Mark Jason Dominus)
    Re: What is perl6? <trondmm-usenet@crusaders.no>
        Why 0640 parameter value in call to tie?? <miscellaneousemail@yahoo.com>
    Re: Why 0640 parameter value in call to tie?? (Garry Williams)
    Re: Why 0640 parameter value in call to tie?? (Mark Jason Dominus)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sat, 07 Jul 2001 15:35:59 GMT
From: Carlos C. Gonzalez <miscellaneousemail@yahoo.com>
Subject: Use of uninitialized value in FORMAT??
Message-Id: <MPG.15b0d9e79b71248c989693@news.edmonton.telusplanet.net>

Hi everyone,

I have read through perldoc perlform, the documentation on formline, have 
read the format section of learning Perl, and have tried various 
constructs of the code below and I do not understand why I am getting an 
error.  Perl reports that I am using an uninitialized value in formline 
(which is a function called by the format statement).  Does anyone have 
any ideas on why this is happening or ideas as to where I could look for 
further insight on this?

I know that $first_name, $date_started, and $tos_version are all being 
assigned correct values because when I take out the comments from the 
prints and comment out the write this code works just fine.

----code

#!/usr/perl/bin -w
use strict;
my %h;
my ($first_name, $date_started, $tos_version);

$h{"johnsmith\@it.com"} = "John Smith,07-05-2001,07-03-2001";
$h{"tammy\@corporate.net"} = "Tammy,07-05-2000,07-03-2000";
$h{"harry\@junk.org"} = "Harry,07-05-1999,07-03-1999";
$h{"tom\@domain.com"} = "Tom,07-05-1998,07-03-1998";

format STDOUT = 
@<<<<<<<<<<<<<<<, @<<<<<<<<<<<<, @<<<<<<<<<<
$first_name, $date_started ,$tos_version
 .
print_unpacked(\%h);

sub print_unpacked
{
  my ($hash) = @_;
  my ($k, $v, $first_name, $date_started, $tos_version);
  while (($k, $v) = each %$hash)
  { 
    print $k."\n";
    ($first_name, $date_started, $tos_version) = split(/,/,$v);
#    print $first_name."\n";
#    print $date_started."\n";
#    print $tos_version."\n";
    write (STDOUT);
  }
}
	
Thanks 

-- 
Carlos 
www.internetsuccess.ca


------------------------------

Date: Sat, 7 Jul 2001 12:45:14 -0400
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Use of uninitialized value in FORMAT??
Message-Id: <slrn9kef4q.pu5.tadmc@tadmc26.august.net>

Carlos C. Gonzalez <miscellaneousemail@yahoo.com> wrote:
>
>I have read through perldoc perlform, the documentation on formline, have 
>read the format section of learning Perl, and have tried various 
>constructs of the code below and I do not understand why I am getting an 
>error.  


Because you have more than one variable named $first_name and
are getting confused between the two.


>Perl reports that I am using an uninitialized value in formline 

>I know that $first_name, $date_started, and $tos_version 


But _which_ $first_name, $date_started, and $tos_version variables?

You have two of each, one file-scoped set (used by write()) and
one scoped to only your subroutine body.


>my ($first_name, $date_started, $tos_version);


Those are your uninitialized values. You never put anything
into them.


>format STDOUT = 
>@<<<<<<<<<<<<<<<, @<<<<<<<<<<<<, @<<<<<<<<<<
>$first_name, $date_started ,$tos_version
>.

>sub print_unpacked
>{
>  my ($hash) = @_;
>  my ($k, $v, $first_name, $date_started, $tos_version);
   ^^
   ^^

These are the ones that you assign to but they are not the
same variables as the ones above, because you _said_ (by
my()ing them) that they are not the same.

Don't say that :-)

Delete the entire line above...


>  while (($k, $v) = each %$hash)

 ... and stick a my() in for these 2 variables:

     while ( my($k, $v) = each %$hash)


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


------------------------------

Date: Sat, 07 Jul 2001 17:35:12 GMT
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re: Use of uninitialized value in FORMAT??
Message-Id: <3B4744BC.617D17EC@rochester.rr.com>

"Carlos C. Gonzalez" wrote:
 ...
> I have read through perldoc perlform, the documentation on formline, have
> read the format section of learning Perl, and have tried various
> constructs of the code below and I do not understand why I am getting an
> error.  Perl reports that I am using an uninitialized value in formline
> (which is a function called by the format statement).  Does anyone have
> any ideas on why this is happening or ideas as to where I could look for
> further insight on this?
> 
> I know that $first_name, $date_started, and $tos_version are all being
> assigned correct values because when I take out the comments from the
> prints and comment out the write this code works just fine.
> 
> ----code
> 
> #!/usr/perl/bin -w
> use strict;
> my %h;
> my ($first_name, $date_started, $tos_version);
> 
> $h{"johnsmith\@it.com"} = "John Smith,07-05-2001,07-03-2001";
> $h{"tammy\@corporate.net"} = "Tammy,07-05-2000,07-03-2000";
> $h{"harry\@junk.org"} = "Harry,07-05-1999,07-03-1999";
> $h{"tom\@domain.com"} = "Tom,07-05-1998,07-03-1998";
> 
> format STDOUT =
> @<<<<<<<<<<<<<<<, @<<<<<<<<<<<<, @<<<<<<<<<<
> $first_name, $date_started ,$tos_version
> .
> print_unpacked(\%h);
> 
> sub print_unpacked
> {
>   my ($hash) = @_;
>   my ($k, $v, $first_name, $date_started, $tos_version);
>   while (($k, $v) = each %$hash)
>   {
>     print $k."\n";
>     ($first_name, $date_started, $tos_version) = split(/,/,$v);
> #    print $first_name."\n";
> #    print $date_started."\n";
> #    print $tos_version."\n";
>     write (STDOUT);
>   }
> }
 ...
> Carlos
 ...
The problem is that you declared $first_name, $date_started, and
$tos_version as lexical variables in your subroutine.  In your main
program, you declared them as lexical variables to your main program. 
The two sets of lexical variables have nothing to do with each other,
even though the names are the same (that being the point of lexical
variables). If you remove those three variables from the my statement in
the subroutine, you will be all set.  When the variables are assigned
values in the subroutine, the format from the main program will see
those values.
-- 
Bob Walton


------------------------------

Date: Sat, 07 Jul 2001 17:36:54 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: Using a hash passed by reference inside a function??
Message-Id: <3b4748b5.5f83$aa@news.op.net>

In article <MPG.15afd4838cbee8d989687@news.edmonton.telusplanet.net>,
Carlos C. Gonzalez  <miscellaneousemail@yahoo.com> wrote:
>How can I use a hash passed by reference (in order to prevent it being 
>flattened into a scalar) inside a subroutine?

You may want to take a look at 

        http://perl.plover.com/FAQs/References.html

It is a very short tutorial about how to use references in Perl.

You may have this on your computer already.  Try

        perldoc perlreftut

-- 
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print


------------------------------

Date: Sat, 07 Jul 2001 18:02:08 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: Using the hash DB_File uses, after untie it??
Message-Id: <3b474e7e.6016$16c@news.op.net>

In article <MPG.15afa1a23401f8f7989682@news.edmonton.telusplanet.net>,
Carlos C. Gonzalez  <miscellaneousemail@yahoo.com> wrote:
>If I untie (close) the file, the hash used to access the file goes away.  
>If I copy the hash to another hash and display the hash copy will that 
>work without keeping the file open through the original hash?

Sure.

        tie %tied, 'DB_File', ...;
        # work with %tied here

        %copy = %tied;
        untie %tied;

        # work with %copy here

-- 
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print


------------------------------

Date: Sat, 07 Jul 2001 13:24:24 GMT
From: "Trond Michelsen" <trondmm-usenet@crusaders.no>
Subject: Re: What is perl6?
Message-Id: <c4E17.8311$qR5.912029@news01.chello.no>

PerlFAQ Server <faq@denver.pm.org> skrev i
meldingsnyheter:wPx17.38$T3.188819456@news.frii.net...
>   What is perl6?
[...]
>     The first alpha release is expected by Summer 2001.

I guess this FAQ needs some adjustment ;-)

--
Trond Michelsen





------------------------------

Date: Sat, 07 Jul 2001 16:09:15 GMT
From: Carlos C. Gonzalez <miscellaneousemail@yahoo.com>
Subject: Why 0640 parameter value in call to tie??
Message-Id: <MPG.15b0e17a75cde8b4989694@news.edmonton.telusplanet.net>

Hi everyone,

I have read through perlopentut, the unmask documentation, the DB_File 
documentation, tried making heads or tails of all the tie documentation, 
and read through the fnctl documentation and was wondering if someone 
could explain to me why there is a value of 0640 passed to the following 
call:

tie %h, "DB_File", $file, O_RDONLY, 0640, $DB_HASH 
    or die "Cannot open $file: $!\n";

This call is from the pages of the DB_File documentation.  

I know this much...that 0640 is a permission passed through the tie 
function to the sysopen function.  I also know that this value is OR'd 
with the unmask value to arrive at the final permission of the file.  

I am wondering though at the need for the 0640 parameter value (what 
would be the default if not provided) and whether there will always be an 
unmask value on UNIX systems that will change whatever permission I 
specify in the tie call.

Thanks
-- 
Carlos 
www.internetsuccess.ca


------------------------------

Date: Sat, 07 Jul 2001 16:59:36 GMT
From: garry@ifr.zvolve.net (Garry Williams)
Subject: Re: Why 0640 parameter value in call to tie??
Message-Id: <slrn9kefvo.h7k.garry@zfw.zvolve.net>

On Sat, 07 Jul 2001 16:09:15 GMT, Carlos C. Gonzalez
<miscellaneousemail@yahoo.com> wrote:

> I have read through perlopentut, the unmask documentation, the DB_File 
> documentation, tried making heads or tails of all the tie documentation, 
> and read through the fnctl documentation and was wondering if someone 
> could explain to me why there is a value of 0640 passed to the following 
> call:
> 
> tie %h, "DB_File", $file, O_RDONLY, 0640, $DB_HASH 
>     or die "Cannot open $file: $!\n";
> 
> This call is from the pages of the DB_File documentation.  
> 
> I know this much...that 0640 is a permission passed through the tie 
> function to the sysopen function.  

Actually, it's passed through to the Berkeley db_open() function.
That function ends up using it in a call to open(2).  Its value is
only used when O_CREAT is specified.  See open(2).  

> I also know that this value is OR'd 
> with the unmask value to arrive at the final permission of the file.  

That's not true.  It is ANDed with the _complement_ of the umask.  And
that's why 0640 is often the wrong choice for creating files.  0666
will become what the user wants.  The user _can_ set the umask to end
up getting 0640 on a created file.  But if the program specifies 0640,
the user cannot get 0660 by setting his umask.  

> I am wondering though at the need for the 0640 parameter value (what 
> would be the default if not provided) 

It's not needed.  When opening files for input, specify 0 to eliminate
confusion.  The value is ignored.  

> and whether there will always be an 
> unmask value on UNIX systems that will change whatever permission I 
> specify in the tie call.

There is always a umask.  Sometimes the user actually sets its value.
Unless your application has special needs for file privacy, use 0666
when creating files so that you end up respecting the user's umask
value.  

-- 
Garry Williams


------------------------------

Date: Sat, 07 Jul 2001 17:13:32 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: Why 0640 parameter value in call to tie??
Message-Id: <3b47431a.5f0e$1e7@news.op.net>

In article <MPG.15b0e17a75cde8b4989694@news.edmonton.telusplanet.net>,
Carlos C. Gonzalez  <miscellaneousemail@yahoo.com> wrote:
>I have read through perlopentut, the unmask documentation, the DB_File 
>documentation, tried making heads or tails of all the tie documentation, 
>and read through the fnctl documentation and was wondering if someone 
>could explain to me why there is a value of 0640 passed to the following 
>call:
>
>tie %h, "DB_File", $file, O_RDONLY, 0640, $DB_HASH 
>    or die "Cannot open $file: $!\n";

It's an error; it should be 0666.  I expect it will be fixed in Perl 5.8.

>I know this much...that 0640 is a permission passed through the tie 
>function to the sysopen function.  

Right.

> I also know that this value is OR'd 
>with the unmask value to arrive at the final permission of the file.  

Not quite.  The actual operation is

        MODE & ~UMASK

So, for example, if the mode is 0640 and the umask is 0077, then the
result is

        0640 & ~0077

which 

        perl -le 'print sprintf "%#o", 0640 & ~ 0077'

tells us is 0600.

It's usually a mistake to use anything other than 0666 as the mode.
The umask represents the user's policy.  If the user wants to have a
permissive policy, say with a umask of 002, the program should respect
that unless there is a good reason not to.  Using a mode of 0640
overrides the user's policy and makes the file not-group-writable,
wvwn though the user might want the file to be group-writable.

>I am wondering though at the need for the 0640 parameter value (what 
>would be the default if not provided) 

According to the manual the default is 0666:

        It is possible to omit some or all of the final 4 parameters in the
        call to C<tie> and let them take default values. As DB_HASH is the most
        common file format used, the call:
        
            tie %A, "DB_File", "filename" ;
        
        is equivalent to:
        
            tie %A, "DB_File", "filename", O_CREAT|O_RDWR, 0666, $DB_HASH ;

Although it doesn't say so, you can also do
        
            tie %A, "DB_File", "filename", O_CREAT|O_RDWR;

and the mode will default to 0666.

>and whether there will always be an unmask value on UNIX systems that
>will change whatever permission I specify in the tie call.

There will always be a umask value on unix systems.  It will always be
applied to the permissions of any file that your program creates.  

-- 
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 1263
***************************************


home help back first fref pref prev next nref lref last post