[21932] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4154 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Nov 21 09:06:59 2002

Date: Thu, 21 Nov 2002 06:05:12 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Thu, 21 Nov 2002     Volume: 10 Number: 4154

Today's topics:
        Basic syntax question on using arrays returned from fun <Ed+nospam@ewildgoose.demon.co.uk@>
    Re: Basic syntax question on using arrays returned from <fxn@hashref.com>
    Re: Basic syntax question on using arrays returned from <koos_pol@NO.nl.JUNK.compuware.MAIL.com>
    Re: Basic syntax question on using arrays returned from <Ed+nospam@ewildgoose.demon.co.uk@>
    Re: Basic syntax question on using arrays returned from <Ed+nospam@ewildgoose.demon.co.uk@>
    Re: Basic syntax question on using arrays returned from <koos_pol@NO.nl.JUNK.compuware.MAIL.com>
    Re: Basic syntax question on using arrays returned from <Ed+nospam@ewildgoose.demon.co.uk@>
    Re: Basic syntax question on using arrays returned from (Tad McClellan)
    Re: Basic syntax question on using arrays returned from (Tad McClellan)
    Re: Basic syntax question on using arrays returned from <koos_pol@NO.nl.JUNK.compuware.MAIL.com>
        BEGIN block and $:: variables <BROWNHIK@Syntegra.Bt.Co.Uk>
    Re: BEGIN block and $:: variables <dave@dave.org.uk>
    Re: BEGIN block and $:: variables (Jay Tilton)
    Re: BEGIN block and $:: variables <BROWNHIK@Syntegra.Bt.Co.Uk>
    Re: BEGIN block and $:: variables <bernard.el-hagin@DODGE_THISlido-tech.net>
    Re: Can someone recommend a beginner's book on Perl? (Randal L. Schwartz)
    Re: Error while executing a program <jurgenex@hotmail.com>
        Format  a one line file to get two columns (Tim Ellaway)
    Re: Format  a one line file to get two columns <koos_pol@NO.nl.JUNK.compuware.MAIL.com>
    Re: Format  a one line file to get two columns (Tad McClellan)
        How to print whatever text in Perl? (Peter Wu)
    Re: How to print whatever text in Perl? <bernard.el-hagin@DODGE_THISlido-tech.net>
    Re: How to print whatever text in Perl? <jurgenex@hotmail.com>
    Re: implementing a self() method for classes <fxn@hashref.com>
    Re: ix:Hash of ix:hashes (Micke)
    Re: passing address between classes - OO type question <tassilo.parseval@post.rwth-aachen.de>
        Perl Module for MS Word.doc on Unix server (Jason Quek)
    Re: Perl Module for MS Word.doc on Unix server <bart.lateur@pandora.be>
    Re: Perl one-liners in Win32 <bart.lateur@pandora.be>
        Problems with FTP - any ideas (Bob Dubery)
    Re: reg exp problem (Tad McClellan)
    Re: regex confusion <bart.lateur@pandora.be>
    Re: Regexp: Clearing captured value <bart.lateur@pandora.be>
    Re: Where do I learn about local web servers? <bart.lateur@pandora.be>
    Re: XML to LDAP Perl Help <not-a-real-address@usa.net>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Thu, 21 Nov 2002 11:53:37 GMT
From: "Edward Wildgoose" <Ed+nospam@ewildgoose.demon.co.uk@>
Subject: Basic syntax question on using arrays returned from function
Message-Id: <5P3D9.726283$5e5.96900@post-02.news.easynews.com>

Lets suppose that I have a function in a package which returns an array, how
can I use the results without assigning to a temporary variable first, ie

my @array = $package->fn;
print $array[0];

 ...is fine, but...

print $package->fn[0]

isn't.  If I actually pass back a reference to the array then things are
fine:

print $package->fn->[0]

The arrays in question are small (2-3 elements), perhaps it is better
practice to return the ref anyway?  For interest, what would be the correct
syntax to use the array from the return result directly.

P.S. let assume an implementation as simple as:

package A

sub A {
    my $class = shift;
    return ('abc','cde');
}


Thanks all,

Ed




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

Date: Thu, 21 Nov 2002 12:14:47 +0000 (UTC)
From: Francesc Xavier Noria <fxn@hashref.com>
Subject: Re: Basic syntax question on using arrays returned from function
Message-Id: <ariinm$c6v$1@news.ya.com>

In article <5P3D9.726283$5e5.96900@post-02.news.easynews.com>, Edward Wildgoose wrote:

> Lets suppose that I have a function in a package which returns an array, how
> can I use the results without assigning to a temporary variable first, ie
> 
> my @array = $package->fn;
> print $array[0];
> 
> ...is fine, but...
> 
> print $package->fn[0]
> 
> isn't.  If I actually pass back a reference to the array then things are
> fine:

Extra parentheses are needed:

    ($package->fn)[0];
    
In case you want to print that take into account this doesn't work:

    print ($package->fn)[0]; # WRONG
    
another pair of parentheses is needed there:

    print(($package->fn)[0]); # RIGHT
    
-- fxn


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

Date: Thu, 21 Nov 2002 13:21:25 +0100
From: Koos Pol <koos_pol@NO.nl.JUNK.compuware.MAIL.com>
Subject: Re: Basic syntax question on using arrays returned from function
Message-Id: <newscache$pbex5h$438$1@news.emea.compuware.com>

Edward Wildgoose wrote (Thursday 21 November 2002 12:53):

> Lets suppose that I have a function in a package which returns an array,
> how can I use the results without assigning to a temporary variable first,
> ie
> 
> my @array = $package->fn;
> print $array[0];
> 
> ...is fine, but...
> 
> print $package->fn[0]


Well, it works if you get rid of the ambiguity:

    print ($package->fn)[0]

Read "perldata" on the gory details of lists and their subscriptions.
If you make clear what what is, you can subscipt endlessly:

    print (($package->fn)[0]->{$key}->[$object_ref]->col())->... etc


> isn't.  If I actually pass back a reference to the array then things are
> fine:
> 
> print $package->fn->[0]


There is no ambiguity here. It can only mean one thing. 

HTH
-- 
KP



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

Date: Thu, 21 Nov 2002 12:27:00 GMT
From: "Edward Wildgoose" <Ed+nospam@ewildgoose.demon.co.uk@>
Subject: Re: Basic syntax question on using arrays returned from function
Message-Id: <oi4D9.742570$DB1.8475007@news.easynews.com>

> Extra parentheses are needed:
>
>     ($package->fn)[0];
>
> In case you want to print that take into account this doesn't work:
>
>     print ($package->fn)[0]; # WRONG
>
> another pair of parentheses is needed there:
>
>     print(($package->fn)[0]); # RIGHT
>

Brilliant!  Thanks for that.  I had experimented with paranthesis, and
nearly got there, but I didn't realise about the extra pair required for
print (which was how I was testing.)

Can you please explain why print needs the whole thing in parenthesis, yet,
a simple assignment doesn't?

ie:
my $answer = ($package->fn)[0]  # OK

Is it good practice to always put brackets around the print statement (ie
make it look like a function)?

Finally, I should clearly have been able to find something as basic as this
in the perl man pages, however, it doesn't really stand out.  Is there a
particular section that covers this syntax? (for those on win32 the HTML
docs by activestate make it VERY easy to browse the man pages - thanks
Activestate!)

Thanks for your help

Ed




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

Date: Thu, 21 Nov 2002 12:39:07 GMT
From: "Edward Wildgoose" <Ed+nospam@ewildgoose.demon.co.uk@>
Subject: Re: Basic syntax question on using arrays returned from function
Message-Id: <Lt4D9.728197$5e5.97121@post-02.news.easynews.com>

> Well, it works if you get rid of the ambiguity:
>
>     print ($package->fn)[0]
>
> Read "perldata" on the gory details of lists and their subscriptions.
> If you make clear what what is, you can subscipt endlessly:
>
>     print (($package->fn)[0]->{$key}->[$object_ref]->col())->... etc
>

This is conecptually REALLY helpful.  Thanks. However, at least in my man
pages, "perldata" does not make this precedence clear (to me).  I get a
little bit more insight from perlref, but I'm still confused on basic stuff
like why "()" rather than "{}"?

For example why is an array qualified like this:
@{$ref})

(Agreed we could write @$ref in this simple case) - Whereas the array
element is then (presumably):

((@{$ref})[0])

Insight would be gratefully appreciated

Thanks

Ed W








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

Date: Thu, 21 Nov 2002 14:28:57 +0100
From: Koos Pol <koos_pol@NO.nl.JUNK.compuware.MAIL.com>
Subject: Re: Basic syntax question on using arrays returned from function
Message-Id: <newscache$9ghx5h$258$1@news.emea.compuware.com>

Edward Wildgoose wrote (Thursday 21 November 2002 13:39):

>> Well, it works if you get rid of the ambiguity:
>>
>>     print ($package->fn)[0]


As already exploited by Francesc Xavier Noria <fxn@hashref.com> this is my 
mistake. (I should have checked my code before posting...)

A second level of ambiguity is introduced by the print statement.
Read on...


> For example why is an array qualified like this:
> @{$ref})


@$ref would suffice.


> 
> (Agreed we could write @$ref in this simple case) - Whereas the array
> element is then (presumably):
> 
> ((@{$ref})[0])


That would still be (@$ref)[0]
This translates into:
Give me a list:     ^^^^^^^
Now get the 0'th element:  ^^^ 

The culprit is print: (from perldoc -f print)
               Because print takes a LIST,
               anything in the LIST is evaluated in list context,
               and any subroutine that you call will have one or
               more of its expressions evaluated in list context.
               Also be careful not to follow the print keyword
               with a left parenthesis unless you want the corre-
               sponding right parenthesis to terminate the argu-
               ments to the print--interpose a "+" or put paren-
               theses around all the arguments.

So although (@$ref)[0] looks like a list subscription, "print" desperately 
tries to print (@$ref) and then doesn't know what to do with the dangling  
[0]. Therefor print needs to be educated:

    print (($package->fn)[0]);  # a list with only one element

or

    print + ($package->fn)[0];  # full evaluation before print



> Insight would be gratefully appreciated
> 
> Thanks
> 
> Ed W


HTH
-- 
KP



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

Date: Thu, 21 Nov 2002 13:44:08 GMT
From: "Edward Wildgoose" <Ed+nospam@ewildgoose.demon.co.uk@>
Subject: Re: Basic syntax question on using arrays returned from function
Message-Id: <Iq5D9.1495888$Xw1.200423@news.easynews.com>

> > (Agreed we could write @$ref in this simple case) - Whereas the array
> > element is then (presumably):
> >
> > ((@{$ref})[0])
>
>
> That would still be (@$ref)[0]
> This translates into:
> Give me a list:     ^^^^^^^
> Now get the 0'th element:  ^^^
>
> The culprit is print: (from perldoc -f print)
>                Because print takes a LIST,
>                anything in the LIST is evaluated in list context,
>                and any subroutine that you call will have one or
>                more of its expressions evaluated in list context.
>                Also be careful not to follow the print keyword
>                with a left parenthesis unless you want the corre-
>                sponding right parenthesis to terminate the argu-
>                ments to the print--interpose a "+" or put paren-
>                theses around all the arguments.
>
> So although (@$ref)[0] looks like a list subscription, "print" desperately
> tries to print (@$ref) and then doesn't know what to do with the dangling
> [0]. Therefor print needs to be educated:
>
>     print (($package->fn)[0]);  # a list with only one element
>
> or
>
>     print + ($package->fn)[0];  # full evaluation before print

Phew!  I get it at last!  Thanks!  Pretty subtle though - I think?

Also, I read man perldata more closely and there is in fact something there
which helps... Sorry...

Now I need to read up on what the "+" does, but I think that is back to me
now.  Thanks for all your help!

Ed




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

Date: Thu, 21 Nov 2002 07:16:27 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Basic syntax question on using arrays returned from function
Message-Id: <slrnatpn5b.2jl.tadmc@magna.augustmail.com>

Edward Wildgoose <Ed+nospam@ewildgoose.demon.co.uk@> wrote:

>> Extra parentheses are needed:
>>
>>     ($package->fn)[0];


That is a "list slice", documented in the "Slices" section in:

   perldoc perldata


>>     print(($package->fn)[0]); # RIGHT
>>
> 
> Brilliant!  Thanks for that.  I had experimented with paranthesis, and
> nearly got there, but I didn't realise about the extra pair required for
> print (which was how I was testing.)
> 
> Can you please explain why print needs the whole thing in parenthesis, yet,
          ^^^^^^^^^^^^^^
> a simple assignment doesn't?


   perldoc -f print

does that. 


   Also be careful not to
   follow the print keyword with a left parenthesis unless you want
   the corresponding right parenthesis to terminate the arguments to
   the print--interpose a C<+> or put parentheses around all the
   arguments.


> Is it good practice to always put brackets around the print statement (ie
> make it look like a function)?


I'd say no. Punctuation "gets in the way" of human understanding,
so "more punctuation" should generally be avoided.

I add it only when I'm required to add it.


> Finally, I should clearly have been able to find something as basic as this
> in the perl man pages, however, it doesn't really stand out.  


You must have missed it there at the end of the 1st paragraph 
of the description for the function that you are using.  :-)


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


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

Date: Thu, 21 Nov 2002 07:32:19 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Basic syntax question on using arrays returned from function
Message-Id: <slrnatpo33.2jl.tadmc@magna.augustmail.com>

Edward Wildgoose <Ed+nospam@ewildgoose.demon.co.uk@> wrote:
>> Well, it works if you get rid of the ambiguity:
>>
>>     print ($package->fn)[0]
>>
>> Read "perldata" on the gory details of lists and their subscriptions.
>> If you make clear what what is, you can subscipt endlessly:
>>
>>     print (($package->fn)[0]->{$key}->[$object_ref]->col())->... etc
>>
> 
> This is conecptually REALLY helpful.  Thanks. However, at least in my man
> pages, "perldata" does not make this precedence clear (to me).  


He referred you to perldata for the "subscripting a list"
(list slice) part.


> I get a
> little bit more insight from perlref, 


perlref would be the place for the "dereferencing" part.


> but I'm still confused on basic stuff
> like why "()" rather than "{}"?


The difference is in what it is that you are disambiguating.

Curlies can disambiguate variable names.

Parenthesis can disambiguate the extent of a function's
argument list.


> For example why is an array qualified like this:
                              ^^^^^^^^^
> @{$ref})


You are not "qualifying" the array there.

You are "dereferencing" a reference to an array.

Or, you are "disambiguating" the variable name.

Has nothing to do with slices.


> (Agreed we could write @$ref in this simple case) - 


Because that case is not ambiguous.


> Whereas the array
> element is then (presumably):
> 
> ((@{$ref})[0])


If it was an actual array, rather than a reference to one,
then that would be:

   ((@array)[0])        # curlies were for the variable name

which is the same as:

   (@array)[0]

which is a "list slice".

For an *array slice* (rather than a list slice) the parens
are not required:

   @array[0]

But you get a warning when you do a 1-element array slice like that,
since

   $array[0]

will select the same element.



So, _none_ of the parenthesis in your example are needed:

   @{$ref}[0]   # an "array slice" via an array reference


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


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

Date: Thu, 21 Nov 2002 15:01:56 +0100
From: Koos Pol <koos_pol@NO.nl.JUNK.compuware.MAIL.com>
Subject: Re: Basic syntax question on using arrays returned from function
Message-Id: <newscache$8zix5h$a58$1@news.emea.compuware.com>

Edward Wildgoose wrote (Thursday 21 November 2002 14:44):

> Now I need to read up on what the "+" does, but I think that is back to me
> now.

"Everything is not as difficult as it seems" -- or sort of.
+ is just plain arithmetic. 

-- 
KP



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

Date: Thu, 21 Nov 2002 11:08:25 -0000
From: "Kevin Brownhill" <BROWNHIK@Syntegra.Bt.Co.Uk>
Subject: BEGIN block and $:: variables
Message-Id: <arif78$fk1$1@pheidippides.axion.bt.co.uk>

Can anyone explain what the following code section does - it is the first
bit of code in a subroutine.

I've looked in all the documentation I can find and still can't understand
it.

BEGIN
 {
 $::pi = 4 * atan2(1,1);
 }


Thanks




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

Date: Thu, 21 Nov 2002 11:31:21 +0000
From: "Dave Cross" <dave@dave.org.uk>
Subject: Re: BEGIN block and $:: variables
Message-Id: <pan.2002.11.21.11.31.21.612732@dave.org.uk>

On Thu, 21 Nov 2002 11:08:25 +0000, Kevin Brownhill wrote:

> Can anyone explain what the following code section does - it is the first
> bit of code in a subroutine.
> 
> I've looked in all the documentation I can find and still can't understand
> it.
> 
> BEGIN
>  {
>  $::pi = 4 * atan2(1,1);
>  }

It sets up a package variable called $pi and gives it the appropriate
value. And it does it and compile time, not runtime.

Dave...

-- 
  ...she opened strange doors that we'd never close again



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

Date: Thu, 21 Nov 2002 11:51:25 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: BEGIN block and $:: variables
Message-Id: <3ddcc62c.370565975@news.erols.com>

"Kevin Brownhill" <BROWNHIK@Syntegra.Bt.Co.Uk> wrote:

: Can anyone explain what the following code section does - it is the first
: bit of code in a subroutine.
: 
: I've looked in all the documentation I can find and still can't understand
: it.
: 
: BEGIN
:  {
:  $::pi = 4 * atan2(1,1);
:  }

The BEGIN{} block causes the code to be executed during the compilation
phase.

$::pi  is  $main::pi, or "variable $pi in the main package".

4 * atan2(1, 1) should be transparent.  It's a common way of getting a
numerical approximation of pi, though one sometimes wishes the original
author had written it as "$pi = atan2(0, -1);".



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

Date: Thu, 21 Nov 2002 11:55:00 -0000
From: "Kevin Brownhill" <BROWNHIK@Syntegra.Bt.Co.Uk>
Subject: Re: BEGIN block and $:: variables
Message-Id: <arihui$h2j$1@pheidippides.axion.bt.co.uk>

Thanks, that explains it.

Does the $::pi package variable need to be declared anywhere with my ?



"Dave Cross" <dave@dave.org.uk> wrote in message
news:pan.2002.11.21.11.31.21.612732@dave.org.uk...
> On Thu, 21 Nov 2002 11:08:25 +0000, Kevin Brownhill wrote:
>
> > Can anyone explain what the following code section does - it is the
first
> > bit of code in a subroutine.
> >
> > I've looked in all the documentation I can find and still can't
understand
> > it.
> >
> > BEGIN
> >  {
> >  $::pi = 4 * atan2(1,1);
> >  }
>
> It sets up a package variable called $pi and gives it the appropriate
> value. And it does it and compile time, not runtime.
>
> Dave...
>
> --
>   ...she opened strange doors that we'd never close again
>




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

Date: Thu, 21 Nov 2002 12:04:34 +0000 (UTC)
From: Bernard El-Hagin <bernard.el-hagin@DODGE_THISlido-tech.net>
Subject: Re: BEGIN block and $:: variables
Message-Id: <arii4i$knc$1@korweta.task.gda.pl>


[please don't top post and please don't quote sigs]


In article <arihui$h2j$1@pheidippides.axion.bt.co.uk>, Kevin Brownhill wrote:
>
> "Dave Cross" <dave@dave.org.uk> wrote in message
> news:pan.2002.11.21.11.31.21.612732@dave.org.uk...
>> On Thu, 21 Nov 2002 11:08:25 +0000, Kevin Brownhill wrote:
>>
>> > Can anyone explain what the following code section does - it is the
> first
>> > bit of code in a subroutine.
>> >
>> > I've looked in all the documentation I can find and still can't
> understand
>> > it.
>> >
>> > BEGIN
>> >  {
>> >  $::pi = 4 * atan2(1,1);
>> >  }
>>
>> It sets up a package variable called $pi and gives it the appropriate
>> value. And it does it and compile time, not runtime.
>
>
> Thanks, that explains it.
> 
> Does the $::pi package variable need to be declared anywhere with my ?


Not if you're explicitly accesing it with the $:: notation.


Cheers,
Bernard
--
echo 42|perl -pe '$#="Just another Perl hacker,"'


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

Date: 21 Nov 2002 05:59:06 -0800
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: Can someone recommend a beginner's book on Perl?
Message-Id: <8665urkor9.fsf@red.stonehenge.com>

>>>>> "Jeff" == Jeff Zucker <jeff@vpservices.com> writes:

Jeff> Hmm, maybe you should have waited until the book came out to learn.
Jeff> It would have been so much easier to learn then.  But writing the book
Jeff> might have been a bit more difficult if you'd done it in that order.

Not if I get a visit from Professor Emmet Brown.

print "Just another Perl hacker," # the original one, that is...

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


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

Date: Thu, 21 Nov 2002 10:01:39 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Error while executing a program
Message-Id: <7a2D9.21278$Q27.4262@nwrddc01.gnilink.net>

Yogen wrote:
> I have just joined the group & just started learning Perl.
> I am facing the following problems.Can anybody help me ?
>
> I.
> When I execute this program, I get the error.
> #! /bin/sh
> #! /usr/local/bin/perl -w
[...]

You should make up your mind if you want your program interpreted by the sh
interpreter or by the perl interpreter.
You can have only one them them.

jue




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

Date: 21 Nov 2002 04:30:43 -0800
From: tellaway@polymersealing.com (Tim Ellaway)
Subject: Format  a one line file to get two columns
Message-Id: <1ffffd4b.0211210430.6f9db003@posting.google.com>

Please could someone tell me how to format a file containing one
continuous line so that there's two columns?

E.g. from this: 

-0.529766666666667 -8.89410873529075 -0.522816666666667
-8.628319980362 0 0 4.9781502 12.02 5.0119999 12.05375

to this:

-0.529766666666667 -8.89410873529075
-0.522816666666667 -8.628319980362
0 0
4.9781502 12.02
5.0119999 12.05375


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

Date: Thu, 21 Nov 2002 14:30:40 +0100
From: Koos Pol <koos_pol@NO.nl.JUNK.compuware.MAIL.com>
Subject: Re: Format  a one line file to get two columns
Message-Id: <newscache$4jhx5h$258$1@news.emea.compuware.com>

Tim Ellaway wrote (Thursday 21 November 2002 13:30):

> Please could someone tell me how to format a file containing one
> continuous line so that there's two columns?
> 
> E.g. from this:
> 
> -0.529766666666667 -8.89410873529075 -0.522816666666667
> -8.628319980362 0 0 4.9781502 12.02 5.0119999 12.05375
> 
> to this:
> 
> -0.529766666666667 -8.89410873529075
> -0.522816666666667 -8.628319980362
> 0 0
> 4.9781502 12.02
> 5.0119999 12.05375

If a space is unambiguously the seperator, lookup the documentation for 
"split".

-- 
KP



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

Date: Thu, 21 Nov 2002 07:43:28 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Format  a one line file to get two columns
Message-Id: <slrnatpoo0.2jl.tadmc@magna.augustmail.com>

Tim Ellaway <tellaway@polymersealing.com> wrote:

> Please could someone tell me how to format a file containing one
> continuous line so that there's two columns?


But the data you showed was NOT on one continuous line.

This will work whether the "parts" are on a single line or not.


----------------------------
#!/usr/bin/perl
use warnings;
use strict;

{ local $/;    # enable "slurp mode"
  $_ = <DATA>;
}

print "$1 $2\n" while /(\S+)\s+(\S+)/g;

__DATA__
-0.529766666666667 -8.89410873529075 -0.522816666666667
-8.628319980362 0 0 4.9781502 12.02 5.0119999 12.05375
----------------------------


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


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

Date: 21 Nov 2002 01:33:04 -0800
From: peterwu@hotmail.com (Peter Wu)
Subject: How to print whatever text in Perl?
Message-Id: <9acc2ac1.0211210133.5e165930@posting.google.com>

Hello,

My program reads the all contents of a ASCII file and prints them out.
I notice that when there is something like $/ $. $), those characters
disappear.

A simple sample:

#!/usr/bin/perl 

$buffer =<<END_OF_BUFFER; 
This is a test message ending with a $. 

END_OF_BUFFER 

print $buffer; # The $ disappears but I want it. :s


I know that they are probably control characters in Perl but how to
print them out "AS IS"? As the file is quite big, is there anyway to
make it happen without doing the substitution of those special
characters? TIA!


- Peter


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

Date: Thu, 21 Nov 2002 09:42:29 +0000 (UTC)
From: Bernard El-Hagin <bernard.el-hagin@DODGE_THISlido-tech.net>
Subject: Re: How to print whatever text in Perl?
Message-Id: <ari9q5$n9h$1@korweta.task.gda.pl>

In article <9acc2ac1.0211210133.5e165930@posting.google.com>, Peter Wu wrote:
> Hello,
> 
> My program reads the all contents of a ASCII file and prints them out.
> I notice that when there is something like $/ $. $), those characters
> disappear.
> 
> A simple sample:
> 
> #!/usr/bin/perl 
> 
> $buffer =<<END_OF_BUFFER; 
> This is a test message ending with a $. 
> 
> END_OF_BUFFER 
> 
> print $buffer; # The $ disappears but I want it. :s
> 
> 
> I know that they are probably control characters in Perl but how to
> print them out "AS IS"? As the file is quite big, is there anyway to
> make it happen without doing the substitution of those special
> characters? TIA!


Either "escape" the special characters, i.e.:

-------
$buffer =<<END_OF_BUFFER;
This is a test message ending with a \$. 

END_OF_BUFFER
-------


or use single quotes:


-------
$buffer =<<'END_OF_BUFFER';
This is a test message ending with a $.

END_OF_BUFFER
-------


Cheers,
Bernard
--
echo 42|perl -pe '$#="Just another Perl hacker,"'


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

Date: Thu, 21 Nov 2002 10:20:35 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: How to print whatever text in Perl?
Message-Id: <Tr2D9.15655$br6.10938@nwrddc03.gnilink.net>

Peter Wu wrote:
> My program reads the all contents of a ASCII file and prints them out.
> I notice that when there is something like $/ $. $), those characters
> disappear.
>
> A simple sample:
>
> #!/usr/bin/perl
>
> $buffer =<<END_OF_BUFFER;
> This is a test message ending with a $.
>
> END_OF_BUFFER
>
> print $buffer; # The $ disappears but I want it. :s

They don't disappear! Those are variable names and they are replaced with
the content of the variable. E.g. $. is the INPUT_LINE_NUMBER which is
probably undefined at that point.
If you would have used warnings then perl would have told you
    Use of uninitialized value in concatenation (.) at ....

> I know that they are probably control characters in Perl but how to
> print them out "AS IS"? As the file is quite big, is there anyway to
> make it happen without doing the substitution of those special
> characters? TIA!

Again, there are no 'control characters' involved here, those are variables.
However it's all about using the proper quoting method.

By default HERE documents (as you used them) ar evaluated as if the text is
enclosed in double quotes. That means you code is the same as (modulo
newlines):

    $buffer ="This is a test message ending with a $.";
    print $buffer; # The $. variable is expanded to its value

If you would have used single quotes, then variables will not be expanded:
    $buffer ='This is a test message ending with a $.';
    print $buffer; # The $. is not evaluated any more

You can achive the same effect for HERE documents by enclosing the name in
single quotes:
$buffer =<<'END_OF_BUFFER';
This is a test message ending with a $.

END_OF_BUFFER

print $buffer; # The $. is not expanded

A totally different approach (while still using double quoted strings) would
be to just escape the $ signs and thereby make them literal symbols.

To learn more about the many different ways to quote a string please see
'perldoc perlop', section Quotes and quote-like Operators.

jue
To learn more about the mny




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

Date: Thu, 21 Nov 2002 09:01:04 +0000 (UTC)
From: Xavier Noria <fxn@hashref.com>
Subject: Re: implementing a self() method for classes
Message-Id: <ari7cg$17u$1@news.ya.com>

In article <pan.2002.11.21.00.02.56.260486.21494@cordata.net>, Eric Anderson wrote:

> On Mon, 18 Nov 2002 15:26:36 -0500, Bryan Castillo wrote:
>> I don't quite understand what you want. The 2nd paragraph says you don't
>> want the object passed as the first argument of the method, but here in
>> the 3rd paragraph you say you will be using it.  Do you mean that you
>> will only use the object passed as the first argument on the first
>> method call?
> 
> Perhaps an example would help:
> 
> # The user creates this
> sub my_method {
> 	print self->attribute1;
> }

I have written this for the fun of trying it, it surely has some pitfalls.
Any comments on how to do the right thing would be very welcomed!

First, this is a method factory:

package MethodFactory;

require Exporter;
use base 'Exporter';
use vars '@EXPORT';

@EXPORT = qw(&defmethod &self);

sub defmethod (*&) {
    my $name = shift;
    my $body = shift;
    my $pkg  = caller;
    eval <<METHOD;
package $pkg;
sub $name {
    local \$self = shift;
    sub self { return \$self };
    &\$body;
}
METHOD
}
	    
1;


Using that module one can define methods like this:

package Foo;
use MethodFactory;

use strict;

sub new {
    my $class = shift;
    my $string = shift;
    return bless { foo => $string}
}
	    
defmethod my_method, sub {
    print self->{foo};
};
		
1;


After that, it seems AFAICT that Foo objects behave normally:

use Foo;

my $foo1 = Foo->new('foo1');
my $foo2 = Foo->new('foo2');
$foo1->my_method;
$foo2->my_method;


Feel free to correct or improve that!

-- fxn


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

Date: 21 Nov 2002 01:30:55 -0800
From: renjuman@hotmail.com (Micke)
Subject: Re: ix:Hash of ix:hashes
Message-Id: <1639fe4b.0211210130.794ae54e@posting.google.com>

renjuman@hotmail.com (Micke) wrote in message news:<1639fe4b.0211200232.5283550e@posting.google.com>...
> Ended up with this (seems to work):
> 
> package Tie2::IIxxHHaasshh;
> use base qw(Tie::IxHash);
> 
> sub FETCH {
>     my $self = $_[0];
>     my $val = $self->SUPER::FETCH($self,$_[1]);
>     return $val if defined $val;
>     tie my(%a), "Tie::IxHash";
>     $self->SUPER::STORE($self,$_[2],\%a);
>     return \%a;
> }
> 1;
> 
> thx

well that did not work.
My main problem was that win32::tie:ini leaked memory so
I switched to Config::IniFiles instead,no more leaks but
now my script was dependent on the way win32::tie:ini
preserved insertion order(Config::IniFiles does not).
I then tried to make a read subroutine to simulate that behavior.
(this thread).My quick fix now is a script self restart every week.:-(


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

Date: 21 Nov 2002 09:06:29 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de>
Subject: Re: passing address between classes - OO type question
Message-Id: <ari7ml$fhj$1@nets3.rz.RWTH-Aachen.DE>

Also sprach Uri Guttman:

>>>>>> "TvP" == Tassilo v Parseval <tassilo.parseval@post.rwth-aachen.de> writes:
> 
>  >> bless $self, $class;
>  >> return $self;
>  >> }
> 
>  TvP> The above is not correct. bless() does not change its first argument in
>  TvP> place. Instead, the blessed reference is what is returned by bless.
>  TvP> Therefore replace the last two or your lines with:
> 
>  TvP>     return bless $self, $class;
> 
> sorry tassilo but you have that wrong and it is a common
> misunderstanding about perl objects. bless affects the *referent* which
> is what the reference points to. otherwise how would this work:
> 
> 	$foo = Bar->new() ;
> 	$bar = $foo();

That's a syntax-error so I don't quite understand your point here.

> 	$bar->method();
> 
> the object is the referent that $foo has a reference too. it must be
> blessed or bar would not refer to a blessed object.
> 
>      bless REF,CLASSNAME
>      bless REF
>              This function tells the thingy referenced by REF
>              that it is now an object in the CLASSNAME package.
>              If CLASSNAME is omitted, the current package is
>              used.  Because a "bless" is often the last thing in
>              a constructor, it returns the reference for
>              convenience.  A

Ah, I didn't know that REF is turned into an object by bless(). I
thought I'd have to use its return value.

> notice the convenience return value and that it blesses the thingy and
> not the reference.
> 
> 'thingy' eq 'referent'.
> 
> damian invented the use of the term referent.

Yet I don't see why this subtle distinction between reference and
referent is needed here. In which way would the (apparently incorrect)
idea which I have of objects so far affect my programs or render them
even unfunctional?

Tassilo
-- 
$_=q!",}])(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus;})(rekcah{lrePbus;})(lreP{rehtonabus;})(rehtona{tsuJbus!;
$_=reverse;s/sub/(reverse"bus").chr(32)/xge;tr~\n~~d;eval;


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

Date: Thu, 21 Nov 2002 10:29:36 GMT
From: jason@generationterrorists.com (Jason Quek)
Subject: Perl Module for MS Word.doc on Unix server
Message-Id: <3de1ab57.16269754@news.starhub.net.sg>

Hi

Is there a module that parses MSWord documents on a Unix server?

I have searched CPAN but cannot seem to find it.

Regards,



Jason Q.


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

Date: Thu, 21 Nov 2002 13:14:25 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: Perl Module for MS Word.doc on Unix server
Message-Id: <jomptuc6h4c1orob96u6kimk3ig4430kk9@4ax.com>

Jason Quek wrote:

>Is there a module that parses MSWord documents on a Unix server?
>
>I have searched CPAN but cannot seem to find it.

Your best bet is the LAOLA project, now also turned into a CPAN module,
OLE::Storage if I recall correctly. Indeed. 

And there appears to be a smaller/reduced OLE::Storage_Lite as well.

-- 
	Bart.


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

Date: Thu, 21 Nov 2002 13:08:48 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: Perl one-liners in Win32
Message-Id: <qfmptucrbsmj8lv1l7n6pind480joa4jpl@4ax.com>

Dave E wrote:

>Of course, I found this one:
>    perl -ne "BEGIN{@ARGV=map{glob}@ARGV}/string_to_find/&&print\"$_\""
>*.*
>which searches for a string in the files specified by the wildcards -
>very useful, but I still can't figure out how it loops through each line
>of every file :)

You got the reason for the loop from Tad. So I won't go into that.

That BEGIN block is necessary because the Windows shell doesn't do any
file globbing by itself. So if you pass "*.txt" as a command line
argument, there will be a bare string "*.txt" in one of the array items
in @ARGV.

Because of the implicit loop, it is wrapped in a BEGIN block, to make
sure it is executed only once, before the loop starts.

And that print \"$_\" is that reallly necessary? Does it do anything a
bare print won't do?

-- 
	Bart.


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

Date: Thu, 21 Nov 2002 12:14:22 GMT
From: megapode@hotmail.com (Bob Dubery)
Subject: Problems with FTP - any ideas
Message-Id: <3ddccb20.365757046@10.100.2.1>

Sometime ago I wrote a program that uses Net::FTP and is invoked by
cron.

It connects to a remote server, CWDs etc, sends all the files in a
stipulated directory (deleting files that are successfully sent) and
maintains a simple audit trail of successes or failures.

This has worked well until about two weeks ago. The audit trails
started filling up with failures instead of successes.

The failures were all failures to write. We could still log into the
machine and CWD.

I found that by FTPing manually from the command line prompt (we are
using SuSE) I could write files.

The admin on the remote site (they are NT) says he has reconfigured
his site and we must be sure to use passive mode.

When I FTP manually it seems to make no difference wether I specify
passive mode or not.

If I use a macro in .netrc then that works OK - wether or not I use
passive mode.

I have tried the following in the program that uses Net::FTP...

1) Use passive->1 when invoking the New method ($session =
Net::FTP->New etc etc)
2) $session->passive()
3) $session->binary()

None of these or any combination seems to make a difference. Net::FTP
can't write, but an FTP session invoked by .netrc or from the shell
can.

The program even works when sending to NT boxes on my network and to
remote unix and even citrix boxes. Just not this particular NT box.

Can anybody shed some light on this or suggest something else I could
try?


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

Date: Wed, 20 Nov 2002 17:46:49 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: reg exp problem
Message-Id: <slrnato7n9.2lv.tadmc@magna.augustmail.com>

jaya prakash <prakashrj@hotmail.com> wrote:
> 
> The following code snippet which was part my program  was giving the
> same output for Diffrent Data Inputs. 


What were you expecting it to do that it is not doing?

It looks straightforward to me, so I can't help explain why
it doesn't do what you expected, because you have neglected
to say what it is that you expected.  :-)


> I am not able to comprehend the
> reason behind it and any help will be greatly appreciated.


Your pattern will match when there are 24 consectutive "t"s
in the string.

There are 24 consecutive "t"s in both strings, so they
both match. What's the problem?

You would need 27 "t"s to get another \1 match, 25 is not enough,
since \1 contains 3 characters.


> #!/usr/bin/perl -w
> 
> my($strict, $sequence, $regexp) = ();


You should declare your variables in the smallest possible scope.
That is often the place where you first use them.


> while(<DATA>) {
>         chomp;


No need to chomp when you are going to strip whitespace later anyway.


>         $sequence .= $_;
> }


You can read the entire file in one go:

   my $sequence;
   { local $/;                # enable slurp mode
     $sequence = <DATA>;      # slurp the entire file
   }


> $sequence =~ s/\s*//g;


Why bother replacing all of those empty string?

    $sequence =~ s/\s+//g;


> $sequence =~ tr/A-Z/a-z/;


That is a fragile way to lowercase something. It does
not respect locales.

   $sequence = lc $sequence;  # much better


> $strict = 8;

   my $strict = 8;


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


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

Date: Thu, 21 Nov 2002 13:02:57 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: regex confusion
Message-Id: <samptu4o9l2o8lck194sbpubmvo7p2ltb6@4ax.com>

Canucklehead wrote:

>Say I have a string containing
>"usr/local/home/someWebsite/foo/htdocs/pdr/xmlData/test/test4.xml" I would
>like to parse out everything except the ending file name (which could be any
>length of characters). In my head I know what needs to be done but
>translating that into perl is another thing. =)

Simple:

	($path) = /(.*)\//;

Take everything up to (but excluding) the last slash.

-- 
	Bart.


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

Date: Thu, 21 Nov 2002 12:32:10 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: Regexp: Clearing captured value
Message-Id: <dfkptucfm2mmhbjpp3d2nfnsiip4fr1pni@4ax.com>

nibl wrote:

>I'm doing several regexp captures on the same data. The result is
>always in $1, but if there is no result then $1 still contains the
>previous result.
>
>How do you clear $1, so you can check whether there is a new result?

Don't rely on $1. Like:

	($one, $two) = /($FOO).*($BAR)/;

$1 will be captured in $one, $2 will be captured in $two. If the match
fails, the regex returns an empty list, and by consequence, $one and
$two will be undef.

-- 
	Bart.


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

Date: Thu, 21 Nov 2002 12:54:11 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: Where do I learn about local web servers?
Message-Id: <8llptuspgr3igld7chsc2h3fvioafa5iaa@4ax.com>

Rob Richardson wrote:

>Apache:  The granddaddy of all web servers.  The web site has lots of
>material.  All of it assumes the user already knows Apache, as far as
>I can tell.  I couldn't find step-by-step instructions on how to
>download and install the Win98 version of Apache.  TinyWeb:  A mention
>in passing in one thread, without a web site.  Another thread seemed
>to say that TinyWeb was severely limited.

That's also Microweb, which might be a (commercial) solution if you plan
on distributing the "intranet" on a CD. 

And, from the same source (<http://www.indigostar.com/>, there's a
distribution of Perl 5.6.1, IndigoPerl, which is compatible with
ActivePerl 5.6.1 and which comes with Apache. No installation headaches
there.

But, actually, installing Apache for Win98 from the original binary
distribution turns out to be quite easy, if you've done some Apache
tweaking before. You can start with the distribution that comes with
IndigoPerl.

-- 
	Bart.


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

Date: 21 Nov 2002 08:49:47 GMT
From: those who know me have no need of my name <not-a-real-address@usa.net>
Subject: Re: XML to LDAP Perl Help
Message-Id: <m1ptszz5n8.gnus@usa.net>

[fu-t set]

in comp.programming i read:

>There is one of 2 ways that we can get information out of our oracle
>data base to import the users in to the directory. A: XML and B: A
>flat file... Can anyone point me in the right direction on were to
>find a convertion tool to convert the xml or the flat file to ldif or
>ldap format? Help please!

there are many perl ldap and xml modules, perhaps XML::Simple and either
Net::LDAP::LDIF or Tie::LDAP would be sufficient.

-- 
bringing you boring signatures for 17 years


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

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 4154
***************************************


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