Defining Global Variables w/use strict;
System: Sun Ultra 2, Solaris 7 (11/99) Perl: 5.6.0 I have a series of related programs that need global definitions ($DOMAIN, $ADMIN, $DBNAME, etc). My code looks something like this: -- #!/usr/local/bin/perl -w use strict; use DBI; require "defs.pl"; print "Welcome to $DOMAIN, $ADMIN\n"; #!/usr/local/bin/perl -w use strict; my $DOMAIN="plexus.com"; my $ADMIN="Chuck"; Of course, when I run g.pl I get the infamous, Global symbol "$DOMAIN" requires explicit package name at ./g.pl line 7. Global symbol "$ADMIN" requires explicit package name at ./g.pl line 7. Execution of ./g.pl aborted due to compilation errors. I've tried "my" and "local", but local just changes the error to complain about an explicit package being required. I'm missing something basic. How can I define these variables to be known in g.pl, h.pl, and other programs that require defs.pl? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Problem with IF statement
An if/else with the same code is pointless. Why not use use the same statement all the time? The %02d won't do anything if both digit places are filled. Printing "5" with %02d comes out 05, printing 25 with %02d comes out 25. What's the point? > -Original Message- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] > Sent: Thursday, November 08, 2001 9:51 AM > To: [EMAIL PROTECTED] > Subject: Re: Problem with IF statement > > > > Just add teh sprintf to the else block too. > > else > { > $year = sprintf("%02d",$now[5] % 100); > } > > -- Rex > > -Original Message- > From: Glenn Cannon [mailto:[EMAIL PROTECTED]] > Sent: Thursday, November 08, 2001 10:47 AM > To: [EMAIL PROTECTED] > Subject: Problem with IF statement > > > I am trying to force the current year to be show as a two digit > number, with > leading zero if necessary. > Here is what I am using currently, what am I doing wrong? > > #Code On > > @now = localtime; > if (length($now[5] % 100) == l) > { > $year = sprintf("%02d", $now[5] % 100) > } > else > { > $year = $now[5] % 100 > } > > #Code Off > > Glenn Cannon > [EMAIL PROTECTED] > Level II Certified DCI Judge > 'There is no spoon.' > www.eventsbeyondbelief.com > > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Defining Global Variables w/use strict;
That's the idea of "defs.pl". It is supposed to define all the globals you'll need. Much like a #include in C. I've done it before in Perl, but I wasn't using -w/use strict. I'm forcing myself to do things the right way, but keep running in to fundamentals I should have learned years ago. Thanks. > -Original Message- > From: dan radom [mailto:[EMAIL PROTECTED]] > Sent: Thursday, November 08, 2001 9:50 AM > To: '[EMAIL PROTECTED]' > Subject: Re: Defining Global Variables w/use strict; > > > just a thought, but how can you print a variable that hasn't > been defined yet? > > dan > > * Tomasi, Chuck ([EMAIL PROTECTED]) wrote: > > System: Sun Ultra 2, Solaris 7 (11/99) > > Perl: 5.6.0 > > > > I have a series of related programs that need global > definitions ($DOMAIN, > > $ADMIN, $DBNAME, etc). My code looks something like this: > > > > -- > > #!/usr/local/bin/perl -w > > > > use strict; > > use DBI; > > > > require "defs.pl"; > > > > print "Welcome to $DOMAIN, $ADMIN\n"; > > > > > > > > #!/usr/local/bin/perl -w > > > > use strict; > > > > my $DOMAIN="plexus.com"; > > my $ADMIN="Chuck"; > > > > > > > > Of course, when I run g.pl I get the infamous, > > > > Global symbol "$DOMAIN" requires explicit package name at > ./g.pl line 7. > > Global symbol "$ADMIN" requires explicit package name at > ./g.pl line 7. > > Execution of ./g.pl aborted due to compilation errors. > > > > I've tried "my" and "local", but local just changes the > error to complain > > about an explicit package being required. I'm missing > something basic. > > > > How can I define these variables to be known in g.pl, h.pl, > and other > > programs that require defs.pl? > > > > -- > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Global Variables and use strict;
Thanks a lot to Jeff 'japhy' Pinyan and Etienne Marcotte for helping me understand packages and variable references today! It's starting to become clear to me now. Thank you. I have to options. Use "require Exporter" and export the variables I would like to, which could be cumbersome if the variable list gets long, and could cause conflicts between exported package variables (globals) and locals of the same name. --or-- Reference the package (global) variables as $pkgname::variable, typing a little more, but avoiding abmiguity. I've used these from other's examples in the past, but now I understand them well enough to construct and use them myself. My Perl knowledge just went up another knotch! Thank you again! --Chuck -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: is there another way to do this?
> die "Only root can run this script" if getpwuid($<) ne 'root'; getpwuid($<) returns the entire passwd entry and would require a little parsing. How about something more like: die "Only root can run this script" if ($< != 0); --Chuck -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
How to pass two hashes to a sub
Perl 5.6.0 Sun Solaris 2.7 I'd like to send two or more associative arrays (hashes) to a sub. It looks like the first one makes it but the values of the second never get passed. Am I doing something wrong or is there a better way to do this (I'm hoping you don't say by reference or I'll have to setup a lot of temporary hashes to pass one or two key/value pairs on second, third, and fourth args) --Code-- #/usr/local/bin/perl -w use strict; sub phash { my (%hash1, %hash2) = @_; print "val1 = $hash1{'val1'}\n"; print "val2 = $hash2{'val2'}\n"; } my %h1 = ( val1 => 56 ); my %h2 = ( val2 => 45 ); &phash(%h1, %h2); output $ perl mhash.pl val1 = 56 val2 = -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: How to pass two hashes to a sub
Arg, I was hoping I wouldn't have to do that (not that I can't), but it would be much easier in some calls. I have a variable like "$ToMailAddr". Rather than setup a hash just to pass the value, it would be simpler for me to say: &mysub(%FromUser, {Address => $ToMailAddr}); where mysub is expecting two hashes, but really only uses the Address field from the second. If it must be references, it must be references... --Chuck > -Original Message- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] > Sent: Thursday, November 15, 2001 12:14 PM > To: Tomasi, Chuck > Cc: '[EMAIL PROTECTED]' > Subject: Re: How to pass two hashes to a sub > > > Unfortunately you HAVE to use references to pass more than > one hash or more > than one array for that matter ... perl flattens the list that are in > arguments so it is impossible to tell where one list starts > and the other > begins etc. I dont get the setting up of temp hashes though > ... what u could > do is this > sub mysub { > my ($h1,$h2) = @_; > #perform your magic on the hash reference instead > } > > my %hash1 = (... > my %hash2 = (... > > &mysub(\%hash1,\%hash2); > > On Thu, Nov 15, 2001 at 11:53:47AM -0600, Tomasi, Chuck > shaped the electrons to read: > > Perl 5.6.0 > > Sun Solaris 2.7 > > > > I'd like to send two or more associative arrays (hashes) to > a sub. It looks > > like the first one makes it but the values of the second > never get passed. > > Am I doing something wrong or is there a better way to do > this (I'm hoping > > you don't say by reference or I'll have to setup a lot of > temporary hashes > > to pass one or two key/value pairs on second, third, and > fourth args) > > > > --Code-- > > #/usr/local/bin/perl -w > > > > use strict; > > > > sub phash > > { > > my (%hash1, %hash2) = @_; > > > > print "val1 = $hash1{'val1'}\n"; > > print "val2 = $hash2{'val2'}\n"; > > } > > > > my %h1 = ( val1 => 56 ); > > my %h2 = ( val2 => 45 ); > > > > &phash(%h1, %h2); > > output > > $ perl mhash.pl > > val1 = 56 > > val2 = > > > > -- > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: How to pass two hashes to a sub
Dang, I could swear I tried that and it wasn't behaving like it was a ref. Now that I've tried it, I'll have to use it that way. Thanks a lot! > -Original Message- > From: Bob Showalter [mailto:[EMAIL PROTECTED]] > Sent: Thursday, November 15, 2001 2:00 PM > To: 'Tomasi, Chuck'; '[EMAIL PROTECTED]' > Subject: RE: How to pass two hashes to a sub > > > > -Original Message- > > From: Tomasi, Chuck [mailto:[EMAIL PROTECTED]] > > Sent: Thursday, November 15, 2001 2:26 PM > > To: '[EMAIL PROTECTED]' > > Cc: '[EMAIL PROTECTED]' > > Subject: RE: How to pass two hashes to a sub > > > > > > Arg, I was hoping I wouldn't have to do that (not that I > > can't), but it > > would be much easier in some calls. I have a variable like > > "$ToMailAddr". > > Rather than setup a hash just to pass the value, it would be > > simpler for me > > to say: > > > > &mysub(%FromUser, {Address => $ToMailAddr}); > > > > where mysub is expecting two hashes, but really only uses the > > Address field > > from the second. If it must be references, it must be references... > > But > >{Address => $ToMailAddr} > > *is* a reference. It's a reference to an anonymous hash. > > So the call > >mysub(\%FromUser, {Address => $ToMailAddr}) > > passes two hash refs to mysub. (Note the \ added to the first arg). > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Populating a referenced hash
Perl: 5.6.0 OS: Solaris 7 Goal: Scan a text file for key words/values and populate a hash My parsing works, but the main() never sees the values properly. If I'm passing by reference, why isn't the hash I passed getting populated in the main namespace? Thanks --Chuck --arg.pl--- #/usr/plx/bin/perl -w use strict; sub ref { my ($href, $aref) =@_; my (@leftovers); foreach (@$aref) { chomp; if (/^UserID\s+:\s+(\d+)/) { ${$href}{'UserID'} = $1; } elsif (/^SupportGroup\s+:\s+(\d+)/) { ${$href}{'SupportGroup'} = $1; } elsif (/^Assigned To\s+:\s+(\d+)/) { ${$href}{'AssignTo'} = $1; } elsif (/^DateOpened\s+:\s+(\d+)/) { ${$href}{'DateOpened'} = $1; } else { push(@leftovers, "$_\n"); } } return(@leftovers); } my @array; my @remains; my (%hash); open(F_TMP, "/tmp/tfile") || die("Cannot open text file"); @array = ; close(F_TMP); @remains = &ref(\%hash, \@array); print "User ID = $hash{'$UserID'}\n"; print "Remains = " . @remains . "\n"; ---output User ID = Remains = 2 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Populating a referenced hash
My mistake on $hash{'$UserID'}. I found that and fixed it shortly after sending the message, but it still doesn't allow me to populate the hash with values. If I were doing this in C, I'd send a structure pointer to the sub/function and get back a the various populated fields. I saw this done a while ago with a hash, but it was using a convention something like: &mysub(*hashname); What's that all about? How are items in %hashname referenced (or dereferenced as the case may be) in mysub()? --Chuck > -Original Message- > From: Wagner-David [mailto:[EMAIL PROTECTED]] > Sent: Friday, November 16, 2001 10:29 AM > To: 'Tomasi, Chuck'; '[EMAIL PROTECTED]' > Subject: RE: Populating a referenced hash > > > Your print using: > print "User ID = $hash{'$UserID'}\n"; > will use $UserID, but there is no such thing. > > In your sub, you are allowing only one value per > assignment, ie your keys are UserID, AssignedTo, etc and > there will be only one value. If you want multiple values, > then will need to do concatentation, but if you are trying to > keep this all together, this may or may not work. > > To see the values you have, use: > foreach (keys %hash) { >printf "%3d %20s\n",$hash{$_}, $_; # prints the hash > value, hash Key > } > > Wags ;) > > -Original Message- > From: Tomasi, Chuck [mailto:[EMAIL PROTECTED]] > Sent: Friday, November 16, 2001 07:29 > To: '[EMAIL PROTECTED]' > Subject: Populating a referenced hash > > > Perl: 5.6.0 > OS: Solaris 7 > Goal: Scan a text file for key words/values and populate a hash > > My parsing works, but the main() never sees the values > properly. If I'm > passing by reference, why isn't the hash I passed getting > populated in the > main namespace? > > Thanks > > --Chuck > > --arg.pl--- > #/usr/plx/bin/perl -w > > use strict; > > sub ref > { > my ($href, $aref) =@_; > my (@leftovers); > > foreach (@$aref) { > chomp; > if (/^UserID\s+:\s+(\d+)/) { > ${$href}{'UserID'} = $1; > } elsif (/^SupportGroup\s+:\s+(\d+)/) { > ${$href}{'SupportGroup'} = $1; > } elsif (/^Assigned To\s+:\s+(\d+)/) { > ${$href}{'AssignTo'} = $1; > } elsif (/^DateOpened\s+:\s+(\d+)/) { > ${$href}{'DateOpened'} = $1; > } else { > push(@leftovers, "$_\n"); > } > } > return(@leftovers); > } > > my @array; > my @remains; > my (%hash); > > open(F_TMP, "/tmp/tfile") || die("Cannot open text file"); > @array = ; > close(F_TMP); > > @remains = &ref(\%hash, \@array); > print "User ID = $hash{'$UserID'}\n"; > print "Remains = " . @remains . "\n"; > ---output > User ID = > Remains = 2 > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Populating a referenced hash
> Your sub looks ok, but of course we can't tell if any of > the regexes are actually matching. The regexes are matching. I put a quick forech() loop to print out the keys and their values. Everything looks OK at the end of the sub. > Instead of ${$href}{'UserID'}, which is valid syntax, the > preferred idiom is: > >$href->{UserID} > > which is a bit easier to read. I'm starting to see that more and more. I'll start using it so I don't look 'outdated'. > You're doing it correctly, so the problem must be > somewhere else. Step through your code in the debugger. Never used it before. I'll see what I can come up with from the help/manpage. Thanks. --Chuck -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Populating a referenced hash
Hash is being populated in the sub, but when I tried accessing the values after calling &ref(), it appeared as they they weren't there. I must have done something silly that I cannot reproduce. Once I stepped through the debugging and saw that %hash had the right values, I wrote another foreach() loop to dump the values after calling &ref(), there they were. Now to see if my larger program adheres to the same logic. You're confused?! I thought I was a decent Perl programmer until I forced myself to use strict and -w! Thanks! --Chuck > -Original Message- > From: Bob Showalter [mailto:[EMAIL PROTECTED]] > Sent: Friday, November 16, 2001 11:29 AM > To: 'Tomasi, Chuck'; '[EMAIL PROTECTED]' > Subject: RE: Populating a referenced hash > > > > -Original Message- > > From: Tomasi, Chuck [mailto:[EMAIL PROTECTED]] > > Sent: Friday, November 16, 2001 12:16 PM > > To: 'Bob Showalter'; '[EMAIL PROTECTED]' > > Subject: RE: Populating a referenced hash > > > > > > > Your sub looks ok, but of course we can't tell if any of > > > the regexes are actually matching. > > > > The regexes are matching. I put a quick forech() loop to > > print out the keys > > and their values. Everything looks OK at the end of the sub. > > Well I'm confused then. I thought the problem was the hash > wasn't being populated. > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Populating a referenced hash
Good point. I should be a little more imaginative. Sometimes making up meaningful variable and function names is the hardest part of writing code. Another thing I found about the references, the order of the parameters matter. If I pass the array ref first and the hash ref second, the foreach(@$aref) loop walks right over the hash ref and I get information at the end of @leftovers "main::hash". --Chuck > -Original Message- > From: Dave Storrs [mailto:[EMAIL PROTECTED]] > Sent: Friday, November 16, 2001 11:38 AM > To: '[EMAIL PROTECTED]' > Subject: RE: Populating a referenced hash > > > > This may or may not solve your problem, but > > Name your sub something other than 'ref'. Ref is a reserved > word in Perl. > > (perldoc -f ref for details on what it does) > > Dave > > > > > --arg.pl--- > > > > #/usr/plx/bin/perl -w > > > > > > > > use strict; > > > > > > > > sub ref > > > > { > > > > my ($href, $aref) =@_; > > > > my (@leftovers); > > > > > > > > foreach (@$aref) { > > > > chomp; > > > > if (/^UserID\s+:\s+(\d+)/) { > > > > ${$href}{'UserID'} = $1; > > > > } elsif (/^SupportGroup\s+:\s+(\d+)/) { > > > > ${$href}{'SupportGroup'} = $1; > > > > } elsif (/^Assigned To\s+:\s+(\d+)/) { > > > > ${$href}{'AssignTo'} = $1; > > > > } elsif (/^DateOpened\s+:\s+(\d+)/) { > > > > ${$href}{'DateOpened'} = $1; > > > > } else { > > > > push(@leftovers, "$_\n"); > > > > } > > > > } > > > > return(@leftovers); > > > > } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Getting past "Use of uninitialized value..."
Perl: 5.6.0 DBI: 1.2.0 DBD::Oracle: 1.12 OS: Solaris 2.7 I'm reading in a list of users from an Oracle database. Some fields are required, some are optional (e.g. Title.) When I get back my populated array of hashes, I have no idea what I got until I print it. Unfortunately, while using strict, I get the dubious message: Use of uninitialized value in concatenation (.) at /path...pl line 1288 every time a record comes up with no title field populated. Is there some way to make this work whether a value was read from the database or not? print "\t\t$ulist[$i]{'Title'}\n"; --Chuck -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Name of current sub?
I find myself writing this sort of thing a lot: print STDERR "program.pl: subname(): debug statement\n"; I know $0 can be used for programname.pl (except it returns the full path to the program), is there some cool variable to get my hands on the name of the current sub? --Chuck -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Name of current sub?
It turns out that (caller 0)[3] got me pretty close to what I was looking for. Thanks. > -Original Message- > From: Bob Showalter [mailto:[EMAIL PROTECTED]] > Sent: Wednesday, November 21, 2001 11:06 AM > To: 'Tomasi, Chuck'; '[EMAIL PROTECTED]' > Subject: RE: Name of current sub? > > > > -----Original Message- > > From: Tomasi, Chuck [mailto:[EMAIL PROTECTED]] > > Sent: Wednesday, November 21, 2001 12:02 PM > > To: '[EMAIL PROTECTED]' > > Subject: Name of current sub? > > > > > > I find myself writing this sort of thing a lot: > > > > print STDERR "program.pl: subname(): debug statement\n"; > > > > I know $0 can be used for programname.pl (except it returns > > the full path to > > the program), is there some cool variable to get my hands on > > the name of the > > current sub? > > perldoc -f caller > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Getting past "Use of uninitialized value..."
I hate those. I usually end up creating a log file (or stderr in the case of web apps) and just print them out to use them again. Nice debugging, but needless in some cases. If you figure a good way use the variable more than once without affecting the value, I'd like to hear it. --Chuck > -Original Message- > From: Etienne Marcotte [mailto:[EMAIL PROTECTED]] > Sent: Wednesday, November 21, 2001 11:15 AM > To: Bob Showalter > Cc: 'Tomasi, Chuck'; '[EMAIL PROTECTED]' > Subject: Re: Getting past "Use of uninitialized value..." > > > can we do the same for "variable used only once" ?? > because I can't get rid of mine > > or I'll remove -w when going into production > > Etienne > > Bob Showalter wrote: > > > > > -Original Message- > > > From: Tomasi, Chuck [mailto:[EMAIL PROTECTED]] > > > Sent: Wednesday, November 21, 2001 11:20 AM > > > To: '[EMAIL PROTECTED]' > > > Subject: Getting past "Use of uninitialized value..." > > > > > > > > > Perl: 5.6.0 > > > DBI: 1.2.0 > > > DBD::Oracle: 1.12 > > > OS: Solaris 2.7 > > > > > > I'm reading in a list of users from an Oracle database. Some > > > fields are > > > required, some are optional (e.g. Title.) When I get back my > > > populated > > > array of hashes, I have no idea what I got until I print it. > > > Unfortunately, > > > while using strict, I get the dubious message: > > > > > > Use of uninitialized value in concatenation (.) at /path...pl > > > line 1288 > > > > > > every time a record comes up with no title field populated. > > > > > > Is there some way to make this work whether a value was > read from the > > > database or not? > > > > > > print "\t\t$ulist[$i]{'Title'}\n"; > > > > Actually, "use strict" isn't the culprit; it's -w or "use warnings". > > > > For 5.6 and higher, you can wrap the code in a block like this: > > > > { > > no warnings 'uninitialized'; > > > > ... your code here ... > > } > > > > -- > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > -- > Etienne Marcotte > Specifications Management - Quality Control > Imperial Tobacco Ltd. - Montreal (Qc) Canada > 514.932.6161 x.4001 > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Re[2]: Getting past "Use of uninitialized value..."
I define global variables in a package (e.g. $MyVars::SystemName = "chuck";). It may only be used in THIS program once, but several other programs need that value. True, modifying it wouldn't be the end of the world, but that could lead to unforeseen problems later if you do something you don't have to. The risk is minimal, but not still there. --Chuck > -Original Message- > From: Daniel Gardner [mailto:[EMAIL PROTECTED]] > Sent: Wednesday, November 21, 2001 12:42 PM > To: Tomasi, Chuck > Cc: '[EMAIL PROTECTED]' > Subject: Re[2]: Getting past "Use of uninitialized value..." > > > Wednesday, November 21, 2001, 6:35:46 PM, Tomasi, Chuck wrote: > > TC> I hate those. I usually end up creating a log file (or > stderr in the case > TC> of web apps) and just print them out to use them again. > Nice debugging, but > TC> needless in some cases. > > TC> If you figure a good way use the variable more than once > without affecting > TC> the value, I'd like to hear it. > > i'm interested - why do you need the variables if you're only using > them once... and what does it matter if you change the value if you're > never going to look at it again? > > > > -- > Best regards, > Danielmailto:[EMAIL PROTECTED] > > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Send e-mail attachment
Here's one I can answer! I agonized over this for weeks, but finally got a decent solution that I use every day! For a binary file I would recommend "use MIME:Base64". An example is provided. Notes: o You'll want to use multi-part MIME encoding (hence the use of MIME::Base64). You could use quotable-printable, but you'll have to do some character translation and it gets yucky real fast. o Take note that the filename is mentioned twice. You can provide that any way you want. o The number in $boundry is up to you. Just make it unique and consistent throughout the message. The number of dashes is fixed and must follow the format given. o Change the path of sendmail to your own sendmail program. -- use MIME::Base64; open(F_MAIL, "|/usr/sbin/sendmail -t"); $boundary="--90125"; print F_MAIL < To: $TOUSER <$TOEMAIL> MIME-Version: 1.0 Subject: File attachment test Content-Type: multipart/mixed; boundary=\"$boundary\" This is a multi-part message in MIME format. --$boundary Content-Type: text/plain; charset=\"iso-8859-1\" Here is the body of the message. A file attachment is also provided below. --$boundary Content-Type: application/octet-stream; name=\"somefile.doc\" Content-Transfer-Encoding: Base64 Content-Disposition: attachment; filename=\"somefile.doc\" END_OF_MAIL open(F_LOG, $LOGFILE) || &Error("Cannot open logfile for MIME encoding"); while (read(F_LOG, $buf, 60*57)) { print F_MAIL encode_base64($buf); } close(F_LOG); print F_MAIL < -Original Message- > From: paul beckett (JIC) [mailto:[EMAIL PROTECTED]] > Sent: Tuesday, November 27, 2001 5:41 AM > To: [EMAIL PROTECTED] > Subject: Send e-mail attachment > Importance: High > > > Does anybody know how I can send a smallish binary file as an e-mail > attachment, from perl (running on UNIX (OSFv.4.0)) > > Thanks > > Paul > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
How to delete a hash entry
I've got hash and I want to completely remove one or more entries from the list. I tried undef, but that removes the value and not the key. %list = ( first => 1, second => 2, third => 3 ) How can I remove $list{second} completely so the following loop only reports first and third? foreach $k (keys %list) { print "$k=$list{$k}\n"; } Thanks --Chuck -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Counting the size of an array reference
For a normal array (@MyArray), one can use $#MyArray to get the position of the last element. So, if I'm given $MyArrayRef, how does one do the same operation to find out how many things are in MyArrayRef? Ex: sub MySub { my ($aref)=@_; return(__); } my @MyArray = (0, 3, 6, 9, 154); my $LastElement=&MySub(\@MyArray); print "The last element is: $LastElement\n"; What goes in ___? --Chuck -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Maintaining a Cache of Hash References
I'm trying to maintain a cache of hashes to reduce database hits. What I want is to determine if I've retrieved the data from the DB before, if so, just pass back the copy of information used last time, otherwise read it from the DB and make a note of it. It would seem I'm not copying the information in to the $user arg properly. Something like this: @UserCache; # Place to store data already seen sub GetUser { my ($id, $user)=@_; # record number and hash reference to populate if (defined($UserCache[$id])) { $user = $UserCache[$id]; return(1); } # Code here to read info from the database and put it in $user as necessary # Store the info from the DB for later $UserCache[$id] = $user; return 1; } Thanks --Chuck -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Maintaining a Cache of Hash References
> >sub GetUser > >{ > > my ($id, $user)=@_; # record number and hash reference to > >populate > > > > if (defined($UserCache[$id])) { > >$user = $UserCache[$id]; > > >return(1); > > > } > > > > # Store the info from the DB for later > > $UserCache[$id] = $user; > > > > return 1; > >} > > To change the value PASSED to the function, you must modify the > corresponding element in @_: > > $_[1] = $UserCache[$id]; Tried it, didn't work. When the select from the database is made, and populates $user->{FieldName}, I'm populating "$user", not $_[1], so why would I have to operate any differently when referencing a different source of the data? I know data is getting in to the cache because $UserCache[$id]->{'Name'} prints out the data I expect. It just isn't getting back to the sub that called GetUser (yes, I checked many times to make sure I'm passing \%user). -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Maintaining a Cache of Hash References
I'll have to keep this in mind. I don't think it applies here since my return value is either a 0 or a 1 (no records read or valid data found). I fill in the hash reference from the database data. It never hurts to learn about a new module! --Chuck > -Original Message- > From: Frank [mailto:[EMAIL PROTECTED]] > Sent: Tuesday, January 15, 2002 11:59 AM > To: '[EMAIL PROTECTED]' > Subject: Re: Maintaining a Cache of Hash References > > > On Tue, Jan 15, 2002 at 11:41:28AM -0600, Tomasi, wrote: > > I'm trying to maintain a cache of hashes to reduce database > hits. What I > > want is to determine if I've retrieved the data from the DB > before, if so, > > just pass back the copy of information used last time, > otherwise read it > > from the DB and make a note of it. It would seem I'm not > copying the > > information in to the $user arg properly. Something like this: > > > ---end quoted text--- > > There is the Memoize module that caches the results of a function > > sub somefunc { > # hit db > return $results; > } > > use Memoize; > memoize('somefunc'); > > IIRC it then seemlessly caches in the background, for DB stuff also > checkout the expiring caches, in case values change in the DB. > > Documentation here: > http://theoryx5.uwinnipeg.ca/CPAN/data/perl/Memoize.html > > -- > Frank Booth - Consultant > Parasol Solutions Limited. > (www.parasolsolutions.com) > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Sorting an array of hashes
Does anyone have any clever ideas for sorting an array of hashes based on a key such as an ID number? Example: @AoH = ( { ID => 10101, UserID => 1041, Status => 2 }, { ID => 10541, UserID => 1211, Status => 1 }, { ID => 10111, UserID => 1211, Status => 2 }, { ID => 10721, UserID => 1198, Status => 1 } ); -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Sorting an array of hashes
Thanks all for the reply! This worked wonderfully. I also tried reverse() and switching the a/b around to get a reverse sorted order with equal success. Thanks again! --Chuck > -Original Message- > From: Nikola Janceski [mailto:[EMAIL PROTECTED]] > Sent: Wednesday, February 06, 2002 1:28 PM > To: 'Tomasi, Chuck'; '[EMAIL PROTECTED]' > Subject: RE: Sorting an array of hashes > > > @sorted = sort { > $a->{ID} <=> $b->{ID} ## remember that $a and $b > become the element > of the array > ## so if it's a reference to a hash use a dereferencer '->' or > # $$a{ID} <=> $$b{ID} > # will work too! > } @AoH; > > foreach $item (@sorted){ > print $item->{ID}, "\n"; > } > > -Original Message- > From: Tomasi, Chuck [mailto:[EMAIL PROTECTED]] > Sent: Wednesday, February 06, 2002 2:18 PM > To: '[EMAIL PROTECTED]' > Subject: Sorting an array of hashes > > > Does anyone have any clever ideas for sorting an array of > hashes based on > a key such as an ID number? > > Example: > > @AoH = ( > { ID => 10101, UserID => 1041, Status => 2 }, > { ID => 10541, UserID => 1211, Status => 1 }, > { ID => 10111, UserID => 1211, Status => 2 }, > { ID => 10721, UserID => 1198, Status => 1 } > ); > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > -- > -- > > The views and opinions expressed in this email message are > the sender's > own, and do not necessarily represent the views and opinions of Summit > Systems Inc. > > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]