[26130] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 8323 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Aug 15 09:05:45 2005

Date: Mon, 15 Aug 2005 06:05:08 -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           Mon, 15 Aug 2005     Volume: 10 Number: 8323

Today's topics:
        2 files-array->1files <kristoff@kristoff.tk>
    Re: 2 files-array->1files <noreply@gunnar.cc>
    Re: 2 files-array->1files <josef.moellers@fujitsu-siemens.com>
    Re: 2 files-array->1files <noreply@gunnar.cc>
        dump text to excel and CPAN search <ngoc@yahoo.com>
    Re: dump text to excel and CPAN search <matthew.garrish@sympatico.ca>
        Eliminate empty captures in a regex? <asdf>
    Re: Eliminate empty captures in a regex? <matthew.garrish@sympatico.ca>
    Re: How do I use global variables safely? <zen13097@zen.co.uk>
    Re: How do I use global variables safely? <djames@thehub.com.au>
    Re: kanjidic parser in Perl? jwb@csse.monash.edu.au
    Re: kanjidic parser in Perl? <usenet@sljfaq.org>
    Re: kanjidic parser in Perl? jwb@csse.monash.edu.au
    Re: kanjidic parser in Perl? <bignose+hates-spam@benfinney.id.au>
    Re: mod_gzip and perl <spam-block-@-SEE-MY-SIG.com>
    Re: mod_gzip and perl <""alexjaquet\"@[no spam]msn.com">
        OSDC 2005 Call For Papers <simon@unisolve.com.au>
    Re: slow substr? <bugbear@trim_papermule.co.uk_trim>
        Very basic question on using references inside function <Mark.Seger@hp.com>
    Re: Very basic question on using references inside func (Anno Siegel)
    Re: Very basic question on using references inside func <noreply@gunnar.cc>
    Re: Very basic question on using references inside func <Mark.Seger@hp.com>
    Re: Very basic question on using references inside func (Anno Siegel)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Mon, 15 Aug 2005 12:53:35 +0200
From: "kristoff" <kristoff@kristoff.tk>
Subject: 2 files-array->1files
Message-Id: <43007438$0$16315$626a14ce@news.free.fr>

 Hi,
I'm new in perl.
I'm facing to a problem which will appear for most of you not really
tricky, but I'm not comfortable with array stuff, so prefer asking
question is case of someone made this in the past.

I have 2 set of files, text files.
1st one with this format ( systemname;message )


bingo;ERROR Can't be reach port is closed
pppp;system OK
typo;System OK
Ray;ERROR Can't be reach port is closed
blush;ERROR Can't be reach port is closed
bingo;system OK


2nd FILE ( domain;system name ):
HP;ppp
HP;bingo
IBM;typo
COMPAQ;blush
DELL;ray


EXPECTED RESULT :
I would like to match data, to a final txt files ( preferably an html
output ) sorted or not :
                     HP
                ppp System OK
                bingo System OK
                     IBM
                typo System OK
                     COMPAQ
                blush ERROR Can't be reach port is closed
                           DELL
                ray ERROR Can't be reach port is closed


Does someone have an idea or already realised this kind of stuff


Many thanks in advance for you help




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

Date: Mon, 15 Aug 2005 12:59:36 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: 2 files-array->1files
Message-Id: <3mbasuF15spkvU1@individual.net>

kristoff wrote:
> I have 2 set of files, text files.
> 1st one with this format ( systemname;message )
> 
> bingo;ERROR Can't be reach port is closed
> pppp;system OK
> typo;System OK
> Ray;ERROR Can't be reach port is closed
> blush;ERROR Can't be reach port is closed
> bingo;system OK
> 
> 2nd FILE ( domain;system name ):
> HP;ppp
> HP;bingo
> IBM;typo
> COMPAQ;blush
> DELL;ray
> 
> EXPECTED RESULT :
> I would like to match data, to a final txt files ( preferably an html
> output ) sorted or not :
>                      HP
>                 ppp System OK
>                 bingo System OK
>                      IBM
>                 typo System OK
>                      COMPAQ
>                 blush ERROR Can't be reach port is closed
>                            DELL
>                 ray ERROR Can't be reach port is closed

What have you tried? Which part of the problem do you have difficulties 
with? Where is the code you have written so far?

Please study the posting guidelines for this group: 
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: Mon, 15 Aug 2005 13:09:31 +0200
From: Josef Moellers <josef.moellers@fujitsu-siemens.com>
Subject: Re: 2 files-array->1files
Message-Id: <ddpsuo$5uu$1@nntp.fujitsu-siemens.com>

kristoff wrote:
>  Hi,
> I'm new in perl.
> I'm facing to a problem which will appear for most of you not really
> tricky, but I'm not comfortable with array stuff, so prefer asking
> question is case of someone made this in the past.
>=20
> I have 2 set of files, text files.
> 1st one with this format ( systemname;message )
>=20
>=20
> bingo;ERROR Can't be reach port is closed
> pppp;system OK
> typo;System OK
> Ray;ERROR Can't be reach port is closed
> blush;ERROR Can't be reach port is closed
> bingo;system OK
>=20
>=20
> 2nd FILE ( domain;system name ):
> HP;ppp
> HP;bingo
> IBM;typo
> COMPAQ;blush
> DELL;ray
>=20
>=20
> EXPECTED RESULT :
> I would like to match data, to a final txt files ( preferably an html
> output ) sorted or not :
>                      HP
>                 ppp System OK
>                 bingo System OK
>                      IBM
>                 typo System OK
>                      COMPAQ
>                 blush ERROR Can't be reach port is closed
>                            DELL
>                 ray ERROR Can't be reach port is closed
>=20
>=20
> Does someone have an idea or already realised this kind of stuff

Take a look at hashes and hashes of hashes.

--=20
Josef M=F6llers (Pinguinpfleger bei FSC)
	If failure had no penalty success would not be a prize
						-- T.  Pratchett



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

Date: Mon, 15 Aug 2005 13:23:18 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: 2 files-array->1files
Message-Id: <3mbc9cF15084gU1@individual.net>

Josef Moellers wrote:
> kristoff wrote:
>> I'm not comfortable with array stuff,
> 
> Take a look at hashes and hashes of hashes.

Yeah, that's _much_ easier.

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: Mon, 15 Aug 2005 12:29:15 +0200
From: ngoc <ngoc@yahoo.com>
Subject: dump text to excel and CPAN search
Message-Id: <43006e6d$1@news.broadpark.no>

Hi
I want to dump tabular text to excel. I searched CPAN, without success. 
I am not looking for Spreadsheet::WriteExcel, but something like

my $ste = Spreadsheet_To_Excel->new('path/abc.xls');
$ste->write_to_excel($text);
$ste->close();

write_to_excel method will automatic parse text and write it to excel

Do someone know package that handle it?

I did not find guide to search at CPAN. Where can I find search rule?
"text to excel"
"text" + "to" + "excel"
"text to spreadsheet"
"text" + "to" + "spreadsheet"

give results that do not contain all words I write.

thanks


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

Date: Mon, 15 Aug 2005 08:01:17 -0400
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: dump text to excel and CPAN search
Message-Id: <du%Le.5938$7R.196345@news20.bellglobal.com>


"ngoc" <ngoc@yahoo.com> wrote in message 
news:43006e6d$1@news.broadpark.no...
> Hi
> I want to dump tabular text to excel. I searched CPAN, without success. I 
> am not looking for Spreadsheet::WriteExcel, but something like
>
> my $ste = Spreadsheet_To_Excel->new('path/abc.xls');
> $ste->write_to_excel($text);
> $ste->close();
>

Searching on your main keywords (excel, tabular) gave as the second result:

http://search.cpan.org/~gwyn/Data-Tabular-Dumper-0.02/Dumper.pm

Matt 




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

Date: Sun, 14 Aug 2005 16:13:39 -0700
From: "Eric" <asdf>
Subject: Eliminate empty captures in a regex?
Message-Id: <XcmdnXbY3fiATWLfRVn-1Q@comcast.com>


Is there any way to eliminate an empty captured subexpression in a regex?

For instance, if I match this data

1,,3

with this regex

(\d?),(\d?),(\d?)

I'll end up with $1='1', $2='', and $3='3'. The result I'd prefer is $1='1', 
$2='3', and $3=undef.

I know, I could do this with some pretty trivial Perl code after performing 
the match. However, I'd like to keep all knowledge about possible variations 
in the data format localized within the regex. 




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

Date: Sun, 14 Aug 2005 20:01:59 -0400
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: Eliminate empty captures in a regex?
Message-Id: <VXQLe.5734$7R.113495@news20.bellglobal.com>


"Eric" <asdf> wrote in message news:XcmdnXbY3fiATWLfRVn-1Q@comcast.com...
>
> Is there any way to eliminate an empty captured subexpression in a regex?
>
> For instance, if I match this data
>
> 1,,3
>
> with this regex
>
> (\d?),(\d?),(\d?)
>
> I'll end up with $1='1', $2='', and $3='3'. The result I'd prefer is 
> $1='1', $2='3', and $3=undef.
>

As a general rule of thumb: if you don't want something, don't capture it:

my $str = '1,,3';

foreach ($str =~ /(\d+),?/g) {
   print "$_\n";
}

If you don't abstract your pattern, you'll wind up with excruciatingly long 
and unreadable regular expressions. Note also that you could probably do 
what you're after much more easily by splitting the string

print join(':', split(/,+/, '1,,3'));

And just in case this turns into a csv file question, see the myriad of 
modules available on CPAN that provide better solutions.

Matt 




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

Date: 15 Aug 2005 07:05:10 GMT
From: Dave Weaver <zen13097@zen.co.uk>
Subject: Re: How do I use global variables safely?
Message-Id: <43003ea5$0$318$da0feed9@news.zen.co.uk>

On Sat, 13 Aug 2005 23:00:03 GMT, Damian James <djames@thehub.com.au> wrote:
> 
>  You can just declare the variable in a scope that both subs can see:
>  #!perl
>  use strict;
>  use warnings;
>  my $var;
>  sub foo { $var = $_[0]; }
>  sub bar { print "$var\n"; }
>  foo('test');
>  bar();
> 

You can refine this idea further, so that the access to the variable is
restricted to certain subs only;

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

{
    # Definition of $colour restricted
    # to the enclosing block;
    my $colour;

    # This sub can see $colour
    sub set_colour {
        $colour = shift;
    }

    # So can this
    sub get_colour {
        return $colour;
    }
}
# No access to $colour below here

sub something_else {
    ...
}



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

Date: Mon, 15 Aug 2005 09:10:38 GMT
From: Damian James <djames@thehub.com.au>
Subject: Re: How do I use global variables safely?
Message-Id: <slrndg0lh9.54t.djames@puli.home>

On 15 Aug 2005 07:05:10 GMT, Dave Weaver said:
> ...
> You can refine this idea further, so that the access to the variable is
> restricted to certain subs only;
[Example with scope restricted to surrounding block]

There's MJD's article on scoping, which is a pretty thorough examination:

  http://perl.plover.com/FAQs/Namespaces.html

Then there are closures:

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

$|=1;
my $t = twirly();
select(undef,undef,undef,1/8),
	print $t->() for 0..50;

sub twirly {
  my $i = 0;
  my @char = @_? @_ : qw{/ - \ |};
  sub { "\b"x($i>0).@char[ $i++ % @char ] }
}

__END__

--Damian


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

Date: Sun, 14 Aug 2005 23:21:37 GMT
From: jwb@csse.monash.edu.au
Subject: Re: kanjidic parser in Perl?
Message-Id: <5mQLe.85048$oJ.62809@news-server.bigpond.net.au>

Apud Ben Bullock <usenet@sljfaq.org> (sci.lang.japan) hoc legimus:
>Does anyone know of a parser for Jim Breen's kanjidic written in Perl?

Why parse kanjidic, when there is an XML edition available? (see
http://www.csse.monash.edu.au/~jwb/kanjidic2/index.html)
There are lashings of XML parsers.

-- 
Jim Breen        http://www.csse.monash.edu.au/~jwb/
Clayton School of Information Technology,
Monash University, VIC 3800, Australia 
$B%8%`!&%V%j!<%s(B@$B%b%J%7%eBg3X(B


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

Date: Mon, 15 Aug 2005 08:54:19 +0900
From: "Ben Bullock" <usenet@sljfaq.org>
Subject: Re: kanjidic parser in Perl?
Message-Id: <ddoljb$a2h$1@caraway.media.kyoto-u.ac.jp>


<jwb@csse.monash.edu.au> wrote in message 
news:5mQLe.85048$oJ.62809@news-server.bigpond.net.au...
> Apud Ben Bullock <usenet@sljfaq.org> (sci.lang.japan) hoc legimus:
>>Does anyone know of a parser for Jim Breen's kanjidic written in Perl?
>
> Why parse kanjidic, when there is an XML edition available? (see
> http://www.csse.monash.edu.au/~jwb/kanjidic2/index.html)

I read that page yesterday and saw the comment

"At this stage the KANJIDIC2 file is officially released, but please 
understand that it is still early days for the project and changes in the 
structure may occur, so don't assume anything is set in concrete if you use 
the file in a project."

So, I assumed the format of kanjidic is more stable.

Can you tell us if the format of the XML kanjidic is likely to change enough 
to break existing software?

Also, while I'm at it, a small erratum. On both the kanjidic and kanjidic2 
documentation pages, De Roo's kanji book is listed as being published by 
Bojinsha, but this should be "Bonjinsha".

> There are lashings of XML parsers.

Not to mention lashings of ginger beer.

I don't know anything about XML, but surely the same amount of parsing work 
is required for either format.

In the end I copied out an old C file from my former cjdic project which 
contained most of the codes for kanjidic, and edited it to parse kanjidic 
completely. The next job is to plug the information into MySQL.

In case anyone's interested, I'm planning to add a kanji dictionary function 
to the slj FAQ. For example, on the list of kokuji,

http://www.sljfaq.org/afaq/kokuji-list.html,

I plan to actually remove all the information in the list except the 
characters themselves, and make each character just a link to my dictionary 
lookup function. Then the user can click the character to find information 
if he or she is interested, or click a link to show the information he or 
she is interested in.




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

Date: Mon, 15 Aug 2005 00:55:06 GMT
From: jwb@csse.monash.edu.au
Subject: Re: kanjidic parser in Perl?
Message-Id: <KJRLe.85300$oJ.64150@news-server.bigpond.net.au>

Apud Ben Bullock <usenet@sljfaq.org> (sci.lang.japan) hoc legimus:
><jwb@csse.monash.edu.au> wrote in message 
>news:5mQLe.85048$oJ.62809@news-server.bigpond.net.au...
>> Apud Ben Bullock <usenet@sljfaq.org> (sci.lang.japan) hoc legimus:
>>>Does anyone know of a parser for Jim Breen's kanjidic written in Perl?
>>
>> Why parse kanjidic, when there is an XML edition available? (see
>> http://www.csse.monash.edu.au/~jwb/kanjidic2/index.html)

>I read that page yesterday and saw the comment

>"At this stage the KANJIDIC2 file is officially released, but please 
>understand that it is still early days for the project and changes in the 
>structure may occur, so don't assume anything is set in concrete if you use 
>the file in a project."

True. In fact the next release will have some DTD changes, and the data
will be in a slightly different format.

>So, I assumed the format of kanjidic is more stable.

Um. Well only up to a point. The order and content of everything between
the first two fields and the start of the readings is not fixed. What
will not change is the one or two letter codes on each field. New ones are
often created in the D* group.

>Can you tell us if the format of the XML kanjidic is likely to change enough 
>to break existing software?

No. I don't know anything about the "existing software". Since kanjidic2
has few entity types and makes a lot of use of attributes, I am told by
XML people that it is kinder on parsers than, say, JMdict.

>Also, while I'm at it, a small erratum. On both the kanjidic and kanjidic2 
>documentation pages, De Roo's kanji book is listed as being published by 
>Bojinsha, but this should be "Bonjinsha".

Thanks.

>In the end I copied out an old C file from my former cjdic project which 
>contained most of the codes for kanjidic, and edited it to parse kanjidic 
>completely. The next job is to plug the information into MySQL.

Should be OK provided you didn't assume an order.

-- 
Jim Breen        http://www.csse.monash.edu.au/~jwb/
Clayton School of Information Technology,
Monash University, VIC 3800, Australia 
$B%8%`!&%V%j!<%s(B@$B%b%J%7%eBg3X(B


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

Date: Mon, 15 Aug 2005 13:41:06 +1000 (EST)
From: Ben Finney <bignose+hates-spam@benfinney.id.au>
Subject: Re: kanjidic parser in Perl?
Message-Id: <ddp2si$cnf$1@rose.polar.local>

In sci.lang.japan, Ben Bullock <usenet@sljfaq.org> wrote:
> <jwb@csse.monash.edu.au> wrote: in message 
> > Why parse kanjidic, when there is an XML edition available?
> > There are lashings of XML parsers.
> 
> Not to mention lashings of ginger beer.

Hurrah!

> I don't know anything about XML, but surely the same amount of
> parsing work is required for either format.

With the important difference that XML documents can be robustly
checked for validity, and then parsed into a traversable document
tree, by any XML library without specific knowledge of KANJIDIC.

And XML libraries, for better or worse, are firmly entrenched in just
about any programming environment these days.

-- 
 \           "Self-respect: The secure feeling that no one, as yet, is |
  `\                                 suspicious."  -- Henry L. Mencken |
_o__)                                                                  |
Ben Finney


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

Date: Mon, 15 Aug 2005 05:35:32 +0100
From: James Taylor <spam-block-@-SEE-MY-SIG.com>
Subject: Re: mod_gzip and perl
Message-Id: <ant150432190fNdQ@riscpc.jtnet>

In article <Pine.LNX.4.62.0508142046410.5058@ppepc56.ph.gla.ac.uk>,
Alan J. Flavell <flavell@ph.gla.ac.uk> wrote:
> 
> On Sun, 14 Aug 2005, James Taylor wrote:
> > 
> > Can you point me to something describing the precise details of
> > the alleged violations? 
> 
> Well, this is rather off-topic for the present group,

Understood.

> I'll give you a couple, at least.

Thanks. Both were shocking. Not only does this mean MSIE fails to
implement the standards correctly but, because of its prominence,
this can only harm the web in general. Let's hope people just
ignore it instead of trying to work around its faults by breaking
their server software.

> though that might not be well received by some 
> web readers who still think that MSIE is a web browser  :-}

Just tell them to upgrade! That would be a sweet irony;
websites that say "This site is optimised for browsers
other than MSIE". Mwahahaha!

-- 
James Taylor, London, UK                              PGP key: 3FBE1BF9
To protect against spam, the address in the "From:" header is not valid.
In any case, you should reply to the group so that everyone can benefit.
If you must send me a private email, use james at oakseed demon co uk.



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

Date: Mon, 15 Aug 2005 10:05:33 +0200
From: Alexandre Jaquet <""alexjaquet\"@[no spam]msn.com">
Subject: Re: mod_gzip and perl
Message-Id: <43004cd0$0$1152$5402220f@news.sunrise.ch>

James Taylor a écrit :
> In article <Pine.LNX.4.62.0508142046410.5058@ppepc56.ph.gla.ac.uk>,
> Alan J. Flavell <flavell@ph.gla.ac.uk> wrote:
> 
>>On Sun, 14 Aug 2005, James Taylor wrote:
>>
>>>Can you point me to something describing the precise details of
>>>the alleged violations? 
>>
>>Well, this is rather off-topic for the present group,
> 
> 
> Understood.
> 
> 
>>I'll give you a couple, at least.
> 
> 
> Thanks. Both were shocking. Not only does this mean MSIE fails to
> implement the standards correctly but, because of its prominence,
> this can only harm the web in general. Let's hope people just
> ignore it instead of trying to work around its faults by breaking
> their server software.
> 
> 
>>though that might not be well received by some 
>>web readers who still think that MSIE is a web browser  :-}
> 
> 
> Just tell them to upgrade! That would be a sweet irony;
> websites that say "This site is optimised for browsers
> other than MSIE". Mwahahaha!
> 
<quote>his site is optimised for browsersother than MSIE</quote>

I will put it on my website lol <O))


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

Date: Mon, 15 Aug 2005 13:43:58 +1000
From: Simon Taylor <simon@unisolve.com.au>
Subject: OSDC 2005 Call For Papers
Message-Id: <ddp382$2fpf$6@otis.netspace.net.au>

OSDC (Open Source Developers' Conference) is a grass-roots/low cost
conference in the style of a YAPC or PyCon. It's organised for
developers, by developers, and we're looking for papers on open
source languages, technologies and tools.

The conference will be held in Melbourne (Monash University's Caulfield
Campus) from the 5th til the 7th of December, 2005.

Last years conference had about 160 people and around 60 papers on a
range of topics - see http://www.osdc.com.au/papers/2004.html for a
list. This list might also be useful if you're looking for ideas on
what sort of thing would be appropriate.

To submit a proposal, get yourself to the www.osdc.com.au website, and
hit the 'Call for papers' link, or go directly to the paper submission
website at http://osdc2005.cgpublisher.com/cfp.html

Key Dates:

Proposals deadline              19th August 2005
Proposal acceptance             12th September 2005
Submission deadline             28th October 2005
Final version for proceedings   15th November 2005
Conference                      5th - 7th December 2005

Please feel free to pass this on to any other people or groups
you think might be interested in submitting a paper! thanks

Hope to see you there!

- Simon Taylor


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

Date: Mon, 15 Aug 2005 12:25:01 +0100
From: bugbear <bugbear@trim_papermule.co.uk_trim>
Subject: Re: slow substr?
Message-Id: <43007b8d$0$1197$ed2619ec@ptn-nntp-reader01.plus.net>

xhoster@gmail.com wrote:
> bugbear <bugbear@trim_papermule.co.uk_trim> wrote:
> 
>>However, 50% of the time is not spent
>>in a class utility; when a method
>>has decided what to take from the class
>>buffer, it calls this:
>>
>>sub use {
>>         my ($self, $l) = @_;
>>         $self->{buf} = substr($self->{buf}, $l);
>>}
>>
>>Which simply notes the fact that $l bytes have been consumed.
> 
> 
> You are forcing Perl to copy the string each time.  Don't do that.
> 
> 
>>As I understand it, substr is a very simple internal
>>"book keeping" method in perl, that shouldn't need to allocate
>>memory, or copy memory.
> 
> 
> It is not the substr that needs to allocate and/or copy, it is the
> assignment that you are doing with the results of the substr.  Use the 4
> argument form of substr, so that it modifies the buffer in place.
> 
> substr($self->{buf}, 0, $l, "");

Please accept my humble apologies for forgetting
I had posted this question.

And please also accept my grateful thanks - that speed
things up nicely.

    BugBear


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

Date: Mon, 15 Aug 2005 05:16:09 -0400
From: Mark Seger <Mark.Seger@hp.com>
Subject: Very basic question on using references inside functions
Message-Id: <43005d5c@usenet01.boi.hp.com>

I'd have thought this would have been answered in some of the 
documentation on using references but if it was I missed it.

I understand that if I want to call a function and pass it a reference 
to a scalar I can then modify that scalar within the function as follows:

sub foo {
   my $ref=shift;
   $$ref='new value';
}

and it works just fine.  However, I want to use that scalar within the 
function many times and don't want to call it $$ref every time because 
it feels clumsy.

Is there some other way?  I thought I could make a reference to that 
reference by something like $var=\$$ref and just access $var but that 
didn't work.  My REAL motive for asking is I have some code that passes 
parameters by value and I'd like to go back and pass them by reference 
without have to change all the other code that touches them.

Then again, maybe the perlish way to do it IS to refer to them 
everywhere as $$ref, $@ref or %$ref just to be clearer about what it 
really going on.

-mark


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

Date: 15 Aug 2005 09:58:34 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Very basic question on using references inside functions
Message-Id: <ddpp0a$aoa$1@mamenchi.zrz.TU-Berlin.DE>

Mark Seger  <Mark.Seger@hp.com> wrote in comp.lang.perl.misc:
> I'd have thought this would have been answered in some of the 
> documentation on using references but if it was I missed it.
> 
> I understand that if I want to call a function and pass it a reference 
> to a scalar I can then modify that scalar within the function as follows:
> 
> sub foo {
>    my $ref=shift;
>    $$ref='new value';
> }
> 
> and it works just fine.  However, I want to use that scalar within the 
> function many times and don't want to call it $$ref every time because 
> it feels clumsy.

It's how it's done, clumsy or not.

> Is there some other way?  I thought I could make a reference to that 
> reference by something like $var=\$$ref and just access $var but that 
> didn't work.

That would give you an additional layer of reference and make the
access more complicated, not less.

> My REAL motive for asking is I have some code that passes 
> parameters by value and I'd like to go back and pass them by reference 
> without have to change all the other code that touches them.

Perl *does* pass scalars "by reference" in the sense that a parameter
value can be changed from within the function.  Example:

    sub foe {
        $_[ 0] = 'new value';
    }
    my $x = 'old value';
    fum( $x);
    print "$x\n";

If you want to access the variable under a meaningful name (instead of
$_[ 0]) you can use a one-shot for loop to get an alias (this is also
called topicalization):

    sub fum {
        for my $variable ( $_[ 0] ) {
            $variable = 'new value';
        }
    }

So references are not really necessary to change a scalar variable
from within a sub. (Aggregates are a different matter.)  However,
a code change will be necessary either way.

Anno
-- 
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article.  Click on 
"show options" at the top of the article, then click on the 
"Reply" at the bottom of the article headers.


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

Date: Mon, 15 Aug 2005 12:53:35 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Very basic question on using references inside functions
Message-Id: <3mbahkF1642m5U1@individual.net>

Mark Seger wrote:
> I understand that if I want to call a function and pass it a reference 
> to a scalar I can then modify that scalar within the function as follows:
> 
> sub foo {
>   my $ref=shift;
>   $$ref='new value';
> }
> 
> and it works just fine.  However, I want to use that scalar within the 
> function many times and don't want to call it $$ref every time because 
> it feels clumsy.

<snip>

> My REAL motive for asking is I have some code that passes 
> parameters by value and I'd like to go back and pass them by reference 
> without have to change all the other code that touches them.

So you don't care whether the passed variable is changed? In that case, 
there is of course the obvious dereference way:

     sub foo {
         my $var = ${ $_[0] };
         $var = 'new value';
     }

OTOH, then you wouldn't gain much from passing references instead of 
values...

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: Mon, 15 Aug 2005 07:49:37 -0400
From: Mark Seger <Mark.Seger@hp.com>
Subject: Re: Very basic question on using references inside functions
Message-Id: <43008153@usenet01.boi.hp.com>


> Perl *does* pass scalars "by reference" in the sense that a parameter
> value can be changed from within the function.  Example:
> 
>     sub foe {
>         $_[ 0] = 'new value';
>     }
>     my $x = 'old value';
>     fum( $x);
>     print "$x\n";
> 
What I meant by 'pass by reference' is that any changes you make in the 
function are reflected back at caller who will then see changes to that 
parameter.  I believe what perl does is 'pass by value', in that it 
makes a copy of the data, thereby allowing you to change it without fear 
of clobbering the orignal value.

> If you want to access the variable under a meaningful name (instead of
> $_[ 0]) you can use a one-shot for loop to get an alias (this is also
> called topicalization):
> 
>     sub fum {
>         for my $variable ( $_[ 0] ) {
>             $variable = 'new value';
>         }
>     
}
I do this all the time for all variables, even in for loops as I find 
references like @_ or $_ read very poorly, but that's me.

-mark


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

Date: 15 Aug 2005 12:09:26 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Very basic question on using references inside functions
Message-Id: <ddq0lm$c9a$2@mamenchi.zrz.TU-Berlin.DE>

Mark Seger  <Mark.Seger@hp.com> wrote in comp.lang.perl.misc:
> 
> > Perl *does* pass scalars "by reference" in the sense that a parameter
> > value can be changed from within the function.  Example:
> > 
> >     sub foe {
> >         $_[ 0] = 'new value';
> >     }
> >     my $x = 'old value';
> >     fum( $x);
> >     print "$x\n";
> > 
> What I meant by 'pass by reference' is that any changes you make in the 
> function are reflected back at caller who will then see changes to that 
> parameter.  I believe what perl does is 'pass by value', in that it 
> makes a copy of the data, thereby allowing you to change it without fear 
> of clobbering the orignal value.

You believe wrong.  The code example above shows that changes to sub
arguments (the elements of @_) are indeed visible to the caller.  The
"call-by-value" thing happens when you assign the content of @_ to
local variables, as in "my ( $x, $y, $z) = @_;".  If you change $x etc.
in the sub body, nothing is changed in the environment.  If you change
$_[ 0] etc. directly, it is.

> > If you want to access the variable under a meaningful name (instead of
> > $_[ 0]) you can use a one-shot for loop to get an alias (this is also
> > called topicalization):
> > 
> >     sub fum {
> >         for my $variable ( $_[ 0] ) {
> >             $variable = 'new value';
> >         }
> >     
> }
> I do this all the time for all variables, even in for loops as I find 
> references like @_ or $_ read very poorly, but that's me.

That's an unrelated point. The aliasing behavior of "for" is the same
whether you use an explicit variable or the default $_.

    sub fum {
        for ( $_[ 0] ) {
            $_ = 'new value';
        }
    }

would have illustrated my point just as well.

Anno
-- 
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article.  Click on 
"show options" at the top of the article, then click on the 
"Reply" at the bottom of the article headers.


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

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.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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


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