From jorgie at missouri.edu Wed May 10 16:25:29 2000 From: jorgie at missouri.edu (Jorgensen, Erik ( IATS )) Date: Wed Aug 4 23:59:53 2004 Subject: Local scope for file handles? Message-ID: <7B5F4A3DED02D411B49B0000E889ECB702019D41@UMC-MAIL02> Hey folks! Is there to locally scope file handles? I use: my $this = 'that'; all the time, so I tried: my FILEHANDEL; But it just generates an error. Help! :) Erik -- Erik Jorgensen Programmer/Analyst - Principle Customer Service and Support Information & Access Technology Services University of Missouri - Columbia (573) 882-5974 www.missouri.edu/~ccjorgie jorgie@missouri.edu From sparling at uclick.com Wed May 10 16:45:30 2000 From: sparling at uclick.com (Doug Sparling) Date: Wed Aug 4 23:59:53 2004 Subject: Local scope for file handles? Message-ID: <004a01bfbac9$11038760$610110ac@uclick.com> /---------------------------------------------- |Hey folks! | |Is there to locally scope file handles? | |I use: | | my $this = 'that'; | |all the time, so I tried: | | my FILEHANDEL; | |But it just generates an error. | |Help! :) | |Erik /----------------------------------------------- Try: -- #!/usr/bin/perl -w sub read_file { my $path = shift; local *FH; # use local, not my open(FH, $path) or die "can't open $path: $!\n"; local $/ = undef; # slurp full file local $_ = ; close (FH); return $_; } $contents = read_file('/etc/passwd'); print "$contents\n"; From HancockDC at missouri.edu Wed May 10 16:55:09 2000 From: HancockDC at missouri.edu (Hancock Jr, Denis C.) Date: Wed Aug 4 23:59:53 2004 Subject: FW: Local scope for file handles? Message-ID: <7B5F4A3DED02D411B49B0000E889ECB7D08E11@UMC-MAIL02> It appears that the Major Domo at Perl Mongers does not like to see certain words in messages. I guess that "sub" is one such word. The code example should have the "#" at the beginning of each line removed. ----------------------------------------- Denis C. Hancock?, Jr.????????? Sr. Scientific Programmer/Analyst 213 Curtis Hall?????????????????????? 573-882-1722 (voice) UMC-Agronomy?????????????????? 573-884-7850 (fax) Columbia MO 65211????????????? HancockDC@missouri.edu -----Original Message----- From: Hancock Jr, Denis C. Sent: Wednesday, May 10, 2000 4:33 PM To: Jorgensen, Erik ( IATS ) Subject: RE: Local scope for file handles? Here is an example for creating local file handles from the Camel Book, page 51: #sub newopen { # my $path = shift; # local *FH; # not my! # open (FH, $path) || return undef; # return *FH; #} #$fh = newopen('/etc/passwd'); ----------------------------------------- Denis C. Hancock?, Jr.????????? Sr. Scientific Programmer/Analyst 213 Curtis Hall?????????????????????? 573-882-1722 (voice) UMC-Agronomy?????????????????? 573-884-7850 (fax) Columbia MO 65211????????????? HancockDC@missouri.edu > -----Original Message----- > From: Jorgensen, Erik ( IATS ) [mailto:jorgie@missouri.edu] > Sent: Wednesday, May 10, 2000 4:25 PM > To: 'columbia-pm-list@hfb.pm.org' > Subject: Local scope for file handles? > > > Hey folks! > > Is there to locally scope file handles? > > I use: > > my $this = 'that'; > > all the time, so I tried: > > my FILEHANDEL; > > But it just generates an error. > > Help! :) > > Erik > > -- > Erik Jorgensen > Programmer/Analyst - Principle > Customer Service and Support > Information & Access Technology Services > University of Missouri - Columbia > (573) 882-5974 > www.missouri.edu/~ccjorgie > jorgie@missouri.edu > From Jay at Buffington.net Wed May 10 17:00:42 2000 From: Jay at Buffington.net (Jay Buffington) Date: Wed Aug 4 23:59:53 2004 Subject: Local scope for file handles? Message-ID: <3919DC0A.C3FBBF7@Buffington.net> You could use Typeglobs, but that's icky, (unless you're pre 5.0 - then you'll have to) The best way to do it is through the FileHandle module. i.e.: #Use FileHandle; # #sub printfile($) { # my $filename = shift; # my $filehandle = FileHandle->new(); # open $filehandle, $filename or die "Can't open $filename"; # # while (<$filehandle>) { print; } #} Hope this helps Jay From scott at elvis.mu.org Wed May 10 22:13:42 2000 From: scott at elvis.mu.org (Scott K. Laws) Date: Wed Aug 4 23:59:53 2004 Subject: Local scope for file handles? In-Reply-To: <7B5F4A3DED02D411B49B0000E889ECB702019D41@UMC-MAIL02> Message-ID: Try this: use Symbol; # Anonymous global symbols my($sym) = gensym; open($sym, "filename"); Regards, Scott On Wed, 10 May 2000, Jorgensen, Erik ( IATS ) wrote: > Hey folks! > > Is there to locally scope file handles? > > I use: > > my $this = 'that'; > > all the time, so I tried: > > my FILEHANDEL; > > But it just generates an error. > > Help! :) > > Erik > > -- > Erik Jorgensen > Programmer/Analyst - Principle > Customer Service and Support > Information & Access Technology Services > University of Missouri - Columbia > (573) 882-5974 > www.missouri.edu/~ccjorgie > jorgie@missouri.edu > > From scott at elvis.mu.org Wed May 10 22:15:08 2000 From: scott at elvis.mu.org (Scott K. Laws) Date: Wed Aug 4 23:59:53 2004 Subject: Local scope for file handles? In-Reply-To: <3919DC0A.C3FBBF7@Buffington.net> Message-ID: The Filehandle module is probbly better than the way i suggested Scott On Wed, 10 May 2000, Jay Buffington wrote: > You could use Typeglobs, but that's icky, (unless you're pre 5.0 - then > you'll have to) > > The best way to do it is through the FileHandle module. i.e.: > > #Use FileHandle; > # > #sub printfile($) { > # my $filename = shift; > # my $filehandle = FileHandle->new(); > # open $filehandle, $filename or die "Can't open $filename"; > # > # while (<$filehandle>) { print; } > #} > > Hope this helps > > Jay > From Jay at Buffington.net Thu May 18 02:46:42 2000 From: Jay at Buffington.net (Jay Buffington) Date: Wed Aug 4 23:59:53 2004 Subject: in one array but not in the other Message-ID: <39239FE2.5E97DE66@Buffington.net> I have two arrays (each has a lot of elements which are strings). I want to know which items are in array1 but not in array2. This is the best I could come up with but there is bound to be a better way: foreach $element (@array1) { $count{$element} += 2; } foreach $element (@array2) { $count{$element}++; } foreach $element (keys %count) { push @array3, $element if ($count{$element} == 1); } Is there a better way to accomplish this? Thanks Jay From scott at elvis.mu.org Wed May 17 15:28:57 2000 From: scott at elvis.mu.org (Scott K. Laws) Date: Wed Aug 4 23:59:53 2004 Subject: in one array but not in the other In-Reply-To: <39239FE2.5E97DE66@Buffington.net> Message-ID: This is probbly not teh best way but ...... $ perl -e ' > @a = (1,2,3,4,5,6,7,8,9); @b = (2,4,6,8); > foreach $a (@a) { > if(!grep(/$a/,@b)) { > push(@l,$a); > } > } > print join(" ",@l) . "\n"; > ' 1 3 5 7 9 also somewhat less redable is: $ perl -e ' @a = (1,2,3,4,5,6,7,8,9); @b = (2,4,6,8); @list = grep { $a = $_; !grep { /$a/ } @b } @a; print join(" ",@list) . "\n"; ' 1 3 5 7 9 Regards, Scott On Thu, 18 May 2000, Jay Buffington wrote: > I have two arrays (each has a lot of elements which are strings). I > want to know which items are in array1 but not in array2. > > This is the best I could come up with but there is bound to be a better > way: > > foreach $element (@array1) { $count{$element} += 2; } > foreach $element (@array2) { $count{$element}++; } > foreach $element (keys %count) { push @array3, $element if > ($count{$element} == 1); } > > Is there a better way to accomplish this? > > Thanks > Jay >