[31900] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 3163 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Oct 7 00:09:30 2010

Date: Wed, 6 Oct 2010 21:09:11 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Wed, 6 Oct 2010     Volume: 11 Number: 3163

Today's topics:
    Re: becoming superuser <m@rtij.nl.invlalid>
    Re: becoming superuser <m@rtij.nl.invlalid>
    Re: becoming superuser <merrilljensen@q.com>
    Re: becoming superuser <m@rtij.nl.invlalid>
    Re: becoming superuser <NoSpamPleaseButThisIsValid3@gmx.net>
    Re: becoming superuser <tzz@lifelogs.com>
    Re: becoming superuser <tadmc@seesig.invalid>
    Re: becoming superuser <sherm.pendley@gmail.com>
    Re: becoming superuser <sherm.pendley@gmail.com>
    Re: becoming superuser <john@example.invalid>
    Re: becoming superuser <ben@morrow.me.uk>
    Re: becoming superuser <john@example.invalid>
    Re: becoming superuser <john@example.invalid>
    Re: becoming superuser <tadmc@seesig.invalid>
    Re: becoming superuser <tadmc@seesig.invalid>
        destroying IO::Handle without close()ing the file <nospam-abuse@ilyaz.org>
    Re: destroying IO::Handle without close()ing the file <ben@morrow.me.uk>
    Re: destroying IO::Handle without close()ing the file <nospam-abuse@ilyaz.org>
    Re: fetching webpage and extracting contents <peter@makholm.net>
    Re: toy list processing problem: collect similar terms sln@netherlands.com
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 6 Oct 2010 09:39:21 +0200
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: becoming superuser
Message-Id: <9mltn7-2l5.ln1@news.rtij.nl>

On Wed, 06 Oct 2010 00:57:22 -0600, Uno wrote:

> chmod 0755 ,'$filename' or die "chmod could not open ", $filename, "
> $!\n";

You are trying to chmod a file named '$filename' (litteral dollar-
filename), while you are trying to chmod $filename (the contents of 
variable filename).

(Besides, see perldoc -q quoting why $filename is better than "$filename" 
in this case.)

HTH,
M4


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

Date: Wed, 6 Oct 2010 10:21:33 +0200
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: becoming superuser
Message-Id: <d5otn7-2l5.ln1@news.rtij.nl>

On Wed, 06 Oct 2010 09:39:21 +0200, Martijn Lievaart wrote:

> On Wed, 06 Oct 2010 00:57:22 -0600, Uno wrote:
> 
>> chmod 0755 ,'$filename' or die "chmod could not open ", $filename, "
>> $!\n";
> 
> You are trying to chmod a file named '$filename' (litteral dollar-
> filename), while you are trying to chmod $filename (the contents of
> variable filename).

Duh.

You are chmodding a file named '$filename' (litteral dollar-
filename), while you are trying to chmod $filename (the contents of 
variable filename).

M4


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

Date: Wed, 06 Oct 2010 02:53:33 -0600
From: Uno <merrilljensen@q.com>
Subject: Re: becoming superuser
Message-Id: <l_idnchlMKCTpDHRnZ2dnUVZ5uWdnZ2d@giganews.com>

Martijn Lievaart wrote:
> On Wed, 06 Oct 2010 09:39:21 +0200, Martijn Lievaart wrote:
> 
>> On Wed, 06 Oct 2010 00:57:22 -0600, Uno wrote:
>>
>>> chmod 0755 ,'$filename' or die "chmod could not open ", $filename, "
>>> $!\n";
>> You are trying to chmod a file named '$filename' (litteral dollar-
>> filename), while you are trying to chmod $filename (the contents of
>> variable filename).
> 
> Duh.
> 
> You are chmodding a file named '$filename' (litteral dollar-
> filename), while you are trying to chmod $filename (the contents of 
> variable filename).


Some mistakes are so freaking obvious in hindsight.


$ perl template1.pl
$ ls -l l2.pl
-rwxr-xr-x 1 dan dan 69 2010-10-06 02:47 l2.pl
$ cat l2.pl
#!/usr/bin/perl
use strict;
use warnings;

print 42;

# perl l2.pl
$ perl l2.pl
42$ cat template1.pl
#!/usr/bin/perl
use strict;
use warnings;

     my $filename = 'l2.pl';
     open my $fh, ">", $filename or die "cannot open $filename: $!";
     print $fh  "#!/usr/bin/perl\n";
     print $fh  "use strict;\n";
     print $fh  "use warnings;\n";
     print $fh " \n";
     print $fh "print 42;\n";
     print $fh " \n";
     print $fh "# perl ", $filename, "\n";
     close($fh);
chmod 0755 , $filename or die "chmod could not open  $filename $!\n";
# perl template1.pl


$

How do I add a newline to the print statement?
-- 
Uno


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

Date: Wed, 6 Oct 2010 11:56:29 +0200
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: becoming superuser
Message-Id: <dnttn7-2l5.ln1@news.rtij.nl>

On Wed, 06 Oct 2010 02:53:33 -0600, Uno wrote:

>      print $fh "print 42;\n";

> How do I add a newline to the print statement?

print $fh "print \"42\\n\";\n";

or

print $fh 'print 42, "\n";', "\n";

or

print $fh 'print "42\n";', "\n";

or

my $line = 'print "42\n";';
print $fh "$line\n";

Whatever you find clearer. However, what about:

print <<EOF
#!/usr/bin/perl
use strict;
use warnings;

print "42\\n";

# perl $filename
EOF

M4


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

Date: Wed, 06 Oct 2010 13:52:02 +0200
From: Wolf Behrenhoff <NoSpamPleaseButThisIsValid3@gmx.net>
Subject: Re: becoming superuser
Message-Id: <4cac62e3$0$6984$9b4e6d93@newsspool4.arcor-online.net>

On 06.10.2010 10:53, Uno wrote:
> How do I add a newline to the print statement?

If using a recent version of Perl 5:
use feature qw(say);
say "This is a like print with a newline added";

Otherwise simply add a "\n" to your print command.



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

Date: Wed, 06 Oct 2010 07:56:39 -0500
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: becoming superuser
Message-Id: <87bp77h43c.fsf@lifelogs.com>

On Wed, 06 Oct 2010 13:52:02 +0200 Wolf Behrenhoff <NoSpamPleaseButThisIsValid3@gmx.net> wrote: 

WB> On 06.10.2010 10:53, Uno wrote:
>> How do I add a newline to the print statement?

WB> If using a recent version of Perl 5:
WB> use feature qw(say);
WB> say "This is a like print with a newline added";

I really like the Modern::Perl module from CPAN which adds `say' and
many other must-have recent Perl 5 features automatically.

Ted


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

Date: Wed, 06 Oct 2010 08:04:26 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: becoming superuser
Message-Id: <slrniaot2p.11u.tadmc@tadbox.sbcglobal.net>

Uno <merrilljensen@q.com> wrote:

> #!/usr/bin/perl
> use strict;
> use warnings;
>
>      my $filename = 'l2.pl';
>      open my $fh, ">", $filename or die "cannot open $filename: $!";
>      print $fh  "#!/usr/bin/perl\n";
>      print $fh  "use strict;\n";
>      print $fh  "use warnings;\n";
>      print $fh " \n";
>      print $fh "print 42;\n";


         print $fh 'print "\n";';


>      print $fh " \n";
>      print $fh "# perl ", $filename, "\n";
>      close($fh);
> chmod 0755 , $filename or die "chmod could not open  $filename $!\n";
> # perl template1.pl
>
>
> $
>
> How do I add a newline to the print statement?


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.


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

Date: Wed, 06 Oct 2010 11:10:55 -0400
From: Sherm Pendley <sherm.pendley@gmail.com>
Subject: Re: becoming superuser
Message-Id: <m2d3rns6f4.fsf@sherm.shermpendley.com>

Uno <merrilljensen@q.com> writes:

> I'm trying to write a template for my garden-variety perl script that
> starts from scratch, and following closely the development of xhmod in
> the camel book p 688:
>
> $ perl template1.pl
> count is 0
> No such file or directory

Note the error message - nothing to do with permissions.

> $ cat l2.pl

Proves that the output was written.

>  my $cnt = chmod 0755 ,'$fh';

You're trying to change permissions on a file named $fh here. That's
the one that doesn't exist, hence the "No such file or directory" error
message above.

> I think the above clearly shows that I don't have superuser
> priveleges.

Nonsense. "No such file or directory" means exactly what it says - it
isn't some secret code word that means something else. If the problem
were lack of privileges, the error message would say so.

sherm--

-- 
Sherm Pendley
                                   <http://camelbones.sourceforge.net>
Cocoa Developer


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

Date: Wed, 06 Oct 2010 11:14:02 -0400
From: Sherm Pendley <sherm.pendley@gmail.com>
Subject: Re: becoming superuser
Message-Id: <m28w2bs69x.fsf@sherm.shermpendley.com>

Uno <merrilljensen@q.com> writes:

> chmod 0755 , $filename or die "chmod could not open  $filename $!\n";
 ...
> How do I add a newline to the print statement?

The same way you add one to a string you're passing to any other function,
such as die().

sherm--

-- 
Sherm Pendley
                                   <http://camelbones.sourceforge.net>
Cocoa Developer


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

Date: Wed, 06 Oct 2010 18:12:40 -0600
From: John Smith <john@example.invalid>
Subject: Re: becoming superuser
Message-Id: <l6qdnVxFdqfkjTDRnZ2dnUVZ5tqdnZ2d@giganews.com>

[This is still OP replying on an experimental user identity]


Martijn Lievaart wrote:

[code elided]

Thanks all for replies.  For the sake of my own pedagogy, I threw them 
all together to see how they would work:

ron@dan-desktop:~/source$ perl template1.pl
This is a like print with a newline added
ron@dan-desktop:~/source$ cat l2.pl
#!/usr/bin/perl
use strict;
use warnings;
print "\n";print "53\n";
print 61, "\n";
print "\n";
# perl l2.pl
ron@dan-desktop:~/source$ perl l2.pl

53
61

ron@dan-desktop:~/source$ cat template1.pl
#!/usr/bin/perl
use strict;
use warnings;
use feature qw(say);

     my $filename = 'l2.pl';
     open my $fh, ">", $filename or die "cannot open $filename: $!";
     print $fh  "#!/usr/bin/perl\n";
     print $fh  "use strict;\n";
     print $fh  "use warnings;\n";
     print $fh 'print "\n";';
     say "This is a like print with a newline added";
     print $fh "print \"53\\n\";\n";
     print $fh 'print 61, "\n";', "\n";
     my $line = 'print "\n";';
     print $fh "$line\n";
     print $fh "# perl ", $filename, "\n";
     close($fh);
     chmod 0755 , $filename or die "chmod could not open  $filename $!\n";
# perl template1.pl


ron@dan-desktop:~/source$

So you can see how the thing is supposed to work.  While 'say' is one 
thing is valid perl, it clearly isn't what I want for this task.


> Whatever you find clearer. However, what about:
> 
> print <<EOF
> #!/usr/bin/perl
> use strict;
> use warnings;
> 
> print "42\\n";
> 
> # perl $filename
> EOF

Martin,

I'm not sure what you intend with EOF.  The idea for me was that I 
wanted a program that creates my perl boilerplate, of which the 
distinctive part might be the last line, that I call the "goocher." 
(I'm unaware of the general meaning of this word.  Before I used it to 
denote a commented-out compilation command line, it was regionally used 
to indicate one's last cigarette in the box, it being the one you would 
not want to give away, because it's the "goocher.")

So in C my last line might be
// gcc -c -Wall -Wextra c1.c

These details are critically important when you're working through the 
difficulties in C or fortran, but in perl, the big advantage is that you 
can cat it out and then have your script command right there for you:

ron@dan-desktop:~/source$ cat l2.pl
#!/usr/bin/perl
use strict;
use warnings;
print "\n";print "53\n";
print 61, "\n";
print "\n";
# perl l2.pl
ron@dan-desktop:~/source$ perl l2.pl

 ...

Thanks for your help!
-- 


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

Date: Thu, 7 Oct 2010 01:46:17 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: becoming superuser
Message-Id: <prhvn7-ogo1.ln1@osiris.mauzo.dyndns.org>


Quoth John Smith <john@example.invalid>:
> 
> ron@dan-desktop:~/source$ cat template1.pl
> #!/usr/bin/perl
> use strict;
> use warnings;
> use feature qw(say);
> 
>      my $filename = 'l2.pl';
>      open my $fh, ">", $filename or die "cannot open $filename: $!";
>      print $fh  "#!/usr/bin/perl\n";
>      print $fh  "use strict;\n";
>      print $fh  "use warnings;\n";
>      print $fh 'print "\n";';
>      say "This is a like print with a newline added";

'say' takes a filehandle in the same way 'print' does, so you probably
want

    say $fh "...";

> Martijn Lievaart wrote:
>
> > Whatever you find clearer. However, what about:
> > 
> > print <<EOF
> > #!/usr/bin/perl
> > use strict;
> > use warnings;
> > 
> > print "42\\n";
> > 
> > # perl $filename
> > EOF
> 
> Martin,
> 
> I'm not sure what you intend with EOF.

The construction is called a 'here doc'. It looks like this:

    <<EOF
        <stuff>
    EOF

where 'EOF' is any word. It's a very strange construction if you're not
familiar with Unix shell, but very useful. The way it works is that
everything from the line after the '<<EOF' up until the next line
containing only 'EOF' is inserted in place of the '<<EOF', including
newlines. That probably wasn't very clear, so I'll give you some
examples:

    my $x = <<FOO;      is equivalent to    my $x = "foo\n";
    foo
    FOO

    my $x = <<FOO . "123";              my $x = "foo\nbar\n" . "123";
    foo
    bar
    FOO

    my $x = <<FOO . <<BAR;              my $x = "foo\n" . "bar\n";
    foo
    FOO
    bar
    BAR

The big advantage is that you can create a multi-line string without
needing "\n"s all over the place, and there's no single-character
delimiter that needs escaping.

> The idea for me was that I 
> wanted a program that creates my perl boilerplate, of which the 
> distinctive part might be the last line, that I call the "goocher." 
> (I'm unaware of the general meaning of this word.  Before I used it to 
> denote a commented-out compilation command line, it was regionally used 
> to indicate one's last cigarette in the box, it being the one you would 
> not want to give away, because it's the "goocher.")
> 
> So in C my last line might be
> // gcc -c -Wall -Wextra c1.c
> 
> These details are critically important when you're working through the 
> difficulties in C or fortran, but in perl, the big advantage is that you 
> can cat it out and then have your script command right there for you:

I'm not quite sure I see the use of this, but it sounds like you want

    print $fh <<PERL;
    #!/usr/bin/perl
    ...
    # perl $filename
    PERL

    # perl template1.pl

That way both files end up with a 'goocher'.

BTW, are you aware that you can put perl switches on the #! line, and
perl will process them even if you don't use the kernel's #! mechanism?
This means there's seldom any need to invoke scripts other than as 'perl
script', since any other switches (-T is most common now, since -w has
been superseded by 'use warnings') can be supplied in the file itself.

Ben



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

Date: Wed, 06 Oct 2010 21:12:26 -0600
From: John Smith <john@example.invalid>
Subject: Re: becoming superuser
Message-Id: <NaKdnfrHK-cHpzDRnZ2dnUVZ5vydnZ2d@giganews.com>

Ben Morrow wrote:

> The construction is called a 'here doc'. It looks like this:
> 
>     <<EOF
>         <stuff>
>     EOF
> 
> where 'EOF' is any word. It's a very strange construction if you're not
> familiar with Unix shell, but very useful. The way it works is that
> everything from the line after the '<<EOF' up until the next line
> containing only 'EOF' is inserted in place of the '<<EOF', including
> newlines. That probably wasn't very clear, so I'll give you some
> examples:
> 
>     my $x = <<FOO;      is equivalent to    my $x = "foo\n";
>     foo
>     FOO
> 
>     my $x = <<FOO . "123";              my $x = "foo\nbar\n" . "123";
>     foo
>     bar
>     FOO
> 
>     my $x = <<FOO . <<BAR;              my $x = "foo\n" . "bar\n";
>     foo
>     FOO
>     bar
>     BAR
> 

I'll try this next.
> The big advantage is that you can create a multi-line string without
> needing "\n"s all over the place, and there's no single-character
> delimiter that needs escaping.

Can't quite get that.  The only thing that is clear in this thread is 
that I don't quite have a feel back for what is literal in perl.

> I'm not quite sure I see the use of this, but it sounds like you want
> 
>     print $fh <<PERL;
>     #!/usr/bin/perl
>     ...
>     # perl $filename
>     PERL
> 
>     # perl template1.pl
> 
> That way both files end up with a 'goocher'.
> 
> BTW, are you aware that you can put perl switches on the #! line, and
> perl will process them even if you don't use the kernel's #! mechanism?
> This means there's seldom any need to invoke scripts other than as 'perl
> script', since any other switches (-T is most common now, since -w has
> been superseded by 'use warnings') can be supplied in the file itself.

Well, if it's got a goocher, then I want it!

I have to interrupt my perl study with real life, and when I end a game, 
I always try to make a last second dunk, but sometimes I stumble and 
hurt myself:


ron@dan-desktop:~/source$ perl template1.pl
ron@dan-desktop:~/source$ cat l2.pl
#!/usr/bin/perl
use strict;
use warnings;
use feature qw(say)
This is a like print with a newline added
print "53\n";
print "\n";
# perl l2.pl
ron@dan-desktop:~/source$ cat template1.pl
#!/usr/bin/perl
use strict;
use warnings;
use feature qw(say);

     my $filename = 'l2.pl';
     open my $fh, ">", $filename or die "cannot open $filename: $!";
     print $fh  "#!/usr/bin/perl\n";
     print $fh  "use strict;\n";
     print $fh  "use warnings;\n";
     print $fh 'use feature qw(say)', "\n";

     say $fh "This is a like print with a newline added";
     print $fh "print \"53\\n\";\n";

     my $line = 'print "\n";';
     print $fh "$line\n";
     print $fh "# perl ", $filename, "\n";
     close($fh);
     chmod 0755 , $filename or die "chmod could not open  $filename $!\n";
# perl template1.pl


ron@dan-desktop:~/source$

If "say" were doing the obvious thing here, what should this look like?
-- 
John Smith


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

Date: Wed, 06 Oct 2010 21:22:12 -0600
From: John Smith <john@example.invalid>
Subject: Re: becoming superuser
Message-Id: <usKdnTbuPLt-oTDRnZ2dnUVZ5j6dnZ2d@giganews.com>

Tad McClellan wrote:
> Uno <merrilljensen@q.com> wrote:
> 
>> #!/usr/bin/perl
>> use strict;
>> use warnings;
>>
>>      my $filename = 'l2.pl';
>>      open my $fh, ">", $filename or die "cannot open $filename: $!";
>>      print $fh  "#!/usr/bin/perl\n";
>>      print $fh  "use strict;\n";
>>      print $fh  "use warnings;\n";
>>      print $fh " \n";
>>      print $fh "print 42;\n";
> 
> 
>          print $fh 'print "\n";';
> 
> 
>>      print $fh " \n";
>>      print $fh "# perl ", $filename, "\n";
>>      close($fh);
>> chmod 0755 , $filename or die "chmod could not open  $filename $!\n";
>> # perl template1.pl
>>
>>
>> $
>>
>> How do I add a newline to the print statement?
> 
> 

How wrong is the statement
'the pragmata in perl are what you "use."'
?
-- 
John Smith


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

Date: Wed, 06 Oct 2010 23:00:24 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: becoming superuser
Message-Id: <slrniaqhit.2ki.tadmc@tadbox.sbcglobal.net>

John Smith <john@example.invalid> wrote:

> #!/usr/bin/perl
> use strict;
> use warnings;
> use feature qw(say);
>
>      my $filename = 'l2.pl';
>      open my $fh, ">", $filename or die "cannot open $filename: $!";
>      print $fh  "#!/usr/bin/perl\n";
>      print $fh  "use strict;\n";
>      print $fh  "use warnings;\n";
>      print $fh 'use feature qw(say)', "\n";
                                    ^^
                                    ^^
You are missing a semicolon after that statement...

>
>      say $fh "This is a like print with a newline added";
>      print $fh "print \"53\\n\";\n";
>
>      my $line = 'print "\n";';
>      print $fh "$line\n";
>      print $fh "# perl ", $filename, "\n";
>      close($fh);
>      chmod 0755 , $filename or die "chmod could not open  $filename $!\n";

> If "say" were doing the obvious thing here, what should this look like?


-----------------------------
#!/usr/bin/perl
use warnings;
use strict;
use feature 'say';

      my $filename = 'l2.pl';
      open my $fh, ">", $filename or die "cannot open $filename: $!";
      say $fh  "#!/usr/bin/perl";
      say $fh  "use strict;";
      say $fh  "use warnings;";
      say $fh 'use feature qw(say);';  # added semicolon

      say $fh "say \"53\";";  # or simply:  say $fh "say 53;";

      my $line = 'say "";';
      say $fh $line;          # no double quotes
      say $fh "# perl ", $filename;
      close($fh);
      chmod 0755 , $filename or die "chmod could not open  $filename $!\n";
-----------------------------


or use a here-doc so your eyes don't cross reading \n's and backslashes:

-----------------------------
#!/usr/bin/perl
use warnings;
use strict;
use feature 'say';

my $filename = 'l2.pl';
open my $fh, ">", $filename or die "cannot open $filename: $!";

print $fh <<ENDPERLPROGRAM;
#!/usr/bin/perl
use strict;
use warnings;
use feature qw(say);
say 53;
say "";
# perl $filename
ENDPERLPROGRAM

close($fh);
chmod 0755 , $filename or die "chmod could not open  $filename $!\n";
-----------------------------



-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.


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

Date: Wed, 06 Oct 2010 23:08:57 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: becoming superuser
Message-Id: <slrniaqi2u.2ki.tadmc@tadbox.sbcglobal.net>

John Smith <john@example.invalid> wrote:
> Tad McClellan wrote:
>> Uno <merrilljensen@q.com> wrote:
>> 
>>> #!/usr/bin/perl
>>> use strict;
>>> use warnings;
>>>
>>>      my $filename = 'l2.pl';
>>>      open my $fh, ">", $filename or die "cannot open $filename: $!";
>>>      print $fh  "#!/usr/bin/perl\n";
>>>      print $fh  "use strict;\n";
>>>      print $fh  "use warnings;\n";
>>>      print $fh " \n";
>>>      print $fh "print 42;\n";
>> 
>> 
>>          print $fh 'print "\n";';
>> 
>> 
>>>      print $fh " \n";
>>>      print $fh "# perl ", $filename, "\n";
>>>      close($fh);
>>> chmod 0755 , $filename or die "chmod could not open  $filename $!\n";
>>> # perl template1.pl
>>>
>>>
>>> $
>>>
>>> How do I add a newline to the print statement?


What does all of that quoted material...


> How wrong is the statement
> 'the pragmata in perl are what you "use."'
> ?


 ... have to do with your question?


The statement is quite wrong.

---------------------
#!/usr/bin/perl
use LWP::Simple;
my $html_page = get 'http://www.perl.org';
print $html_page;
---------------------


LWP::Simple is not a pragma.


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.


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

Date: Wed, 6 Oct 2010 22:27:51 +0000 (UTC)
From: Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: destroying IO::Handle without close()ing the file
Message-Id: <slrniaptv7.u24.nospam-abuse@powdermilk.math.berkeley.edu>

The comment in IO/Handle.pm says:

   # There is no need for DESTROY to do anything, because when the
   # last reference to an IO object is gone, Perl automatically
   # closes its associated files (if any).  However, to avoid any
   # attempts to autoload DESTROY, we here define it to do nothing.

So how do people destroy the handle without close()ing?  I think some
POSIX magic should be done, but which one?

Puzzled,
Ilya

P.S.  WHY?

      Suppose I make a same-fd duplicate (open "<&=11").  Now I want
      to get rid of it - but I must not close() the original handle -
      hence not close() the duplicate IO::Handle.


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

Date: Wed, 6 Oct 2010 23:57:23 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: destroying IO::Handle without close()ing the file
Message-Id: <jfbvn7-h4n1.ln1@osiris.mauzo.dyndns.org>


Quoth Ilya Zakharevich <nospam-abuse@ilyaz.org>:
> The comment in IO/Handle.pm says:
> 
>    # There is no need for DESTROY to do anything, because when the
>    # last reference to an IO object is gone, Perl automatically
>    # closes its associated files (if any).  However, to avoid any
>    # attempts to autoload DESTROY, we here define it to do nothing.
> 
> So how do people destroy the handle without close()ing?  I think some
> POSIX magic should be done, but which one?
> 
> P.S.  WHY?
> 
>       Suppose I make a same-fd duplicate (open "<&=11").  Now I want
>       to get rid of it - but I must not close() the original handle -
>       hence not close() the duplicate IO::Handle.

Looking at Perl_io_close in doio.c suggests using

    binmode $FH, ":pop" while PerlIO::get_layers $FH;

before the handle goes out of scope. After this fileno $FH returns -1,
and ktrace confirms that closing it doesn't attempt to call close(2).

I believe, though I haven't tried it, that you could also do this in a
DESTROY method, since they are called before io_close for IO objects.

I don't think there's any other way to destroy the Perl-level object
without closing the OS fd; I don't think normal C stdio provides one
either, which is probably why Perl doesn't.

Ben



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

Date: Thu, 7 Oct 2010 02:33:36 +0000 (UTC)
From: Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: destroying IO::Handle without close()ing the file
Message-Id: <slrniaqcc0.up4.nospam-abuse@powdermilk.math.berkeley.edu>

On 2010-10-06, Ben Morrow <ben@morrow.me.uk> wrote:
>> So how do people destroy the handle without close()ing?

> Looking at Perl_io_close in doio.c suggests using
>
>     binmode $FH, ":pop" while PerlIO::get_layers $FH;
>
> before the handle goes out of scope. After this fileno $FH returns -1,
> and ktrace confirms that closing it doesn't attempt to call close(2).

Thanks for a wonderful piece of voodoo!
Ilya


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

Date: Wed, 06 Oct 2010 09:31:03 +0200
From: Peter Makholm <peter@makholm.net>
Subject: Re: fetching webpage and extracting contents
Message-Id: <87eic322x4.fsf@vps1.hacking.dk>

alfonsobaldaserra <alfonso.baldaserra@gmail.com> writes:

> though i am inclined towards peter's advise to use html parsers.
> unfortunately, i couldn't get your code to work due to lack of usage
> examples of html::treebuilder online.

Huh?

http://www.perlmonks.org/?node_id=280461
http://search.cpan.org/perldoc?HTML::TreeBuilder
http://groups.google.com/group/comp.lang.perl.misc/msg/372b363f0e9be360

//Makholm


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

Date: Wed, 06 Oct 2010 10:52:19 -0700
From: sln@netherlands.com
Subject: Re: toy list processing problem: collect similar terms
Message-Id: <2obpa6hn77qhr6hqprtm4ptofr3r7e8t1l@4ax.com>

On Sat, 25 Sep 2010 21:05:13 -0700 (PDT), Xah Lee <xahlee@gmail.com> wrote:

>here's a interesting toy list processing problem.
>
>I have a list of lists, where each sublist is labelled by
>a number. I need to collect together the contents of all sublists
>sharing
>the same label. So if I have the list
>
>((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q
>r) (5 s t))
>
>where the first element of each sublist is the label, I need to
>produce:
>
>output:
>((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))
>
[snip]

>anyone care to give a solution in Python, Perl, javascript, or other
>lang? am guessing the scheme solution can be much improved... perhaps
>using some lib but that seems to show scheme is pretty weak if the lib
>is non-standard.
>

Crossposting to Lisp, Python and Perl because the weird list of lists looks
like Lisp or something else, and you mention other languages so I'm throwing
this out for Perl.

It appears this string you have there is actually list syntax in another language.
If it is, its the job of the language to parse the data out. Why then do you
want to put it into another language form? At runtime, once the data is in variables,
dictated by the syntax, you can do whatever data manipulation you want
(combining arrays, etc..).

So, in the spirit of a preprocessor, given that the text is balanced, with proper closure,
ie:   ( (data) (data) )    is ok.
      ( data (data) )      is not ok.

the below does simple text manipulation, joining like labeled sublists, without going into
the runtime guts of internalizing the data itself. Internally, this is too simple.

-sln
-----------------
Alternate input:
(
  (
    (0 a b) (1 c d) (2 e f )
  )
  (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q r) (5 s t)
)
------------------
use strict;
use warnings;

my $input = <<EOI;
((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q r)
 (5 s t))
EOI
my $output = $input;

my $regxout = qr/
  ( (?: \( \s* [^()]+ \s* \) (\s*) )+ )
/x;


$output =~ 
s{ $regxout }
 {
    my ( $list, $format ) = ( $1, $2 );
    my ( %hseen,
         @order,
         $replace
    );
    while ($list =~  /\(\s* (\S+) \s* (.+?) \s*\)/xsg) {
        if ( exists $hseen{$1} ) {
            $hseen{$1} .= " $2";
            next;
        }
        push @order, $1;
        $hseen{$1} = $2;
    }
    for my $id (@order) {
        $replace .= "($hseen{$id}) ";
    }
    $replace =~ s/ $//;
    $replace . $format
 }xeg;

print "Input  -\n$input\n";
print "Output -\n$output";

__END__

Input  -
((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q r)
 (5 s t))

Output -
((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))




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

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:

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

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#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 V11 Issue 3163
***************************************


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