That code ( foreach $file(@files) { )was an example *of* use not an example *to* use. To help understand what referneces are. I use a similar idea but it checks for existing names so as to avoid the problem mentioned below. Obviously as with any programming it's good to avoid code that can cause problems. Will this ease your mind?
foreach $file(@files) { if(-e $file) { print "Oh my god this file exists! What ever shall we do? "; } else { open file... @$file = <FILEHANDLE>; close file... } } -----Original Message----- From: Todd Wade [mailto:[EMAIL PROTECTED]] Sent: Saturday, January 04, 2003 4:00 PM To: [EMAIL PROTECTED] Subject: Re: References ch 8 programming Perl "John W. Krahn" <[EMAIL PROTECTED]> wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... My thoughts on perl references. This code: > foreach $file(@files) { > > open file... > @$file = <FILEHANDLE>; > close file... > } creates named arrays via symbolic references. [trwww@devel_rh trwww]$ perl $symRef = "array"; @$symRef = (1,2,3); print( join( "\n", @array ), "\n" ); Ctrl-D 1 2 3 The array has a name, we just accessed it at runtime instead of hardcoding the name in the program. This is illegal with strict, for good reason. for instance, in the foreach $file( @files ) { ... code above, imagine what could happen if one of the files in the directory had the same name as an array in the program!!! If there was a file named 'files' in the directory, the program would probably crash!!! Why do that to your self when you dont need to? see: http://perl.plover.com/varvarname.html > > On Fri, Jan 03, 2003 at 03:33:30PM -0800, John W. Krahn wrote: > > > Paul Kraus wrote: > > > > > > > > Ok a couple questions on Ref from pg 251 programming Perl. > > > > > > > > push @$arrrayref,$filename); > > > > $$arrayref[0]="January"; > > > > @$arrayref[4..6]=qw/May June July/; > > > > > > > > So this is actually creating an anonymous array that it then references > > > > correct? > > > > so the assignments January ect are being made to an anonymous > > > > array. > > > > > > No, an anonymous array is delimited by [ and ]. That is creating > > > an actual array that is only accessible through an array > > > reference. Anonymous arrays are sometimes created with [ and ]. The above snippet of code is dereferencing the array so it can access it. If $arrayref stores a reference ( which stringified looks like ARRAY(0x80f560c) ) and the array was never given a name, then it is an anonymous array... if it stores a string, it is a plain 'ol array being accessed symbollically. Another way to create an anonymous data structure is by returning a reference ( using \ ) of a lexically scoped data structure from a subroutine. This and autovivification are probably two of the coolest features of perl and are why one can write 10 lines of perl code that would usually take about 100 in other languages. This cant be done in C++: [trwww@devel_rh trwww]$ perl use strict; my($var) = getRef(); sub getRef { my(@var) = (1,2,3); return( \@var ); } print $var->[1], "\n"; Ctrl-D 2 you would have to call new(), and then remember to delete() it. anyways, > > > my $ref = [ 1, 2, 3, 4, 5 ]; > > > ^^^^^^^^^^^^^^^^^ > > > anonymous array creates an anonymous array... it has no name. > > > > Not worth picking a fight over, to be sure, but I don't see the > > difference. Assuming $arrayref didn't exist before, then after each > > of those three examples it will, and it will be a reference to an > > array with no name. Seems to me that Paul got it spot on. > > You are saying that it has no name but it does: arrayref. If it truly > had "no name" then there would be no way to access it anywhere else in > the program. > The referent that the contents of $arrayref refers to has no name. There is no place in the program we can access the contents of the memory, at least directly. We have to dereference the reference stored in $arrayref to access it. Todd W. -- 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]