[12172] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 5772 Volume: 8

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue May 25 10:07:21 1999

Date: Tue, 25 May 99 07:01:28 -0700
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Tue, 25 May 1999     Volume: 8 Number: 5772

Today's topics:
    Re: reading a file and replace lines (Tad McClellan)
    Re: reading a file and replace lines <tchrist@mox.perl.com>
        removings " "s from strings <daniel.vesma@thewebtree.com>
    Re: removings " "s from strings <ebohlman@netcom.com>
    Re: removings " "s from strings (Real)
    Re: removings " "s from strings <daniel.vesma@thewebtree.com>
    Re: removings " "s from strings <tchrist@mox.perl.com>
        req: directional help <office@asc.nl>
    Re: req: directional help (Tad McClellan)
        Return of array <kar@webline.dk>
    Re: String search problem (Tad McClellan)
    Re: Using Perl over server, Help <gellyfish@gellyfish.com>
    Re: XS programming (HELP!) newbie... <emilio_tunon@nl.compuware.com>
        Special: Digest Administrivia (Last modified: 12 Dec 98 (Perl-Users-Digest Admin)

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

Date: Tue, 25 May 1999 04:04:31 -0400
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: reading a file and replace lines
Message-Id: <fildi7.itj.ln@magna.metronet.com>

lasteyrie@iname.com wrote:

: and i want my script to replace $Name by is value $Name in my program :

: How can i do that ?


  What is wrong with the way given in Perl FAQ, part 4?

     "How can I expand variables in text strings?"


: thanx

   Uh huh.


--
    Tad McClellan                          SGML Consulting
    tadmc@metronet.com                     Perl programming
    Fort Worth, Texas


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

Date: 25 May 1999 07:46:26 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: reading a file and replace lines
Message-Id: <374aa9b2@cs.colorado.edu>

     [courtesy cc of this posting mailed to cited author]

Cursed by Mozilla/4.04 [fr] (WinNT; I ;Nav),
In comp.lang.perl.misc lasteyrie@iname.com writes:

:I Got a file (not made by me) within line like that :
:"
:<HTML>
:Name : $Name<BR>
:</HTML>
:"
:
:and i want my script to replace $Name by is value $Name in my program :
:for example:
:$Name="Lasteyrie"
:The program must return :
:"
:<HTML>
:Name : Lasteyrie<BR>
:</HTML>

Stop putting variables in non-program text!  What is wrong with the
world that I have to post this every day?  I have never seen a C++
programmer with this mindset -- where do these folks come from?

=head2 How can I use a variable as a variable name?

Beginners often think they want to have a variable contain the name
of a variable.

    $fred    = 23;
    $varname = "fred";
    ++$$varname;         # $fred now 24

This works I<sometimes>, but it is a very bad idea for two reasons.

The first reason is that they I<only work on global variables>.
That means above that if $fred is a lexical variable created with my(),
that the code won't work at all: you'll accidentally access the global
and skip right over the private lexical altogether.  Global variables
are bad because they can easily collide accidentally and in general make
for non-scalable and confusing code.

Symbolic references are forbidden under the C<use strict> pragma.
They are not true references and consequently are not reference counted
or garbage collected.

The other reason why using a variable to hold the name of another
variable a bad idea is that the question often stems from a lack of
understanding of Perl data structures, particularly hashes.  By using
symbolic references, you are just using the package's symbol-table hash
(like C<%main::>) instead of a user-defined hash.  The solution is to
use your own hash or a real reference instead.

    $fred    = 23;
    $varname = "fred";
    $USER_VARS{$varname}++;  # not $$varname++

There we're using the %USER_VARS hash instead of symbolic references.
Sometimes this comes up in reading strings from the user with variable
references and wanting to expand them to the values of your perl
program's variables.  This is also a bad idea because it conflates the
program-addressable namespace and the user-addressable one.  Instead of
reading a string and expanding it to the actual contents of your program's
own variables:

    $str = 'this has a $fred and $barney in it';
    $str =~ s/(\$\w+)/$1/eeg;		  # need double eval

Instead, it would be better to keep a hash around like %USER_VARS and have
variable references actually refer to entries in that hash:

    $str =~ s/\$(\w+)/$USER_VARS{$1}/g;   # no /e here at all

That's faster, cleaner, and safer than the previous approach.  Of course,
you don't need to use a dollar sign.  You could use your own scheme to
make it less confusing, like bracketed percent symbols, etc.

    $str = 'this has a %fred% and %barney% in it';
    $str =~ s/%(\w+)%/$USER_VARS{$1}/g;   # no /e here at all

Another reason that folks sometimes think they want a variable to contain
the name of a variable is because they don't know how to build proper
data structures using hashes.  For example, let's say they wanted two
hashes in their program: %fred and %barney, and to use another scalar
variable to refer to those by name.

    $name = "fred";
    $$name{WIFE} = "wilma";     # set %fred

    $name = "barney";           
    $$name{WIFE} = "betty";	# set %barney

This is still a symbolic reference, and is still saddled with the
problems enumerated above.  It would be far better to write:

    $folks{"fred"}{WIFE}   = "wilma";
    $folks{"barney"}{WIFE} = "betty";

And just use a multilevel hash to start with.

The only times that you absolutely I<must> use symbolic references are
when you really must refer to the symbol table.  This may be because it's
something that can't take a real reference to, such as a format name.
Doing so may also be important for method calls, since these always go
through the symbol table for resolution.

In those cases, you would turn off C<strict 'refs'> temporarily so you
can play around with the symbol table.  For example:

    @colors = qw(red blue green yellow orange purple violet);
    for my $name (@colors) {
        no strict 'refs';  # renege for the block
        *$name = sub { "<FONT COLOR='$name'>@_</FONT>" };
    } 

All those functions (red(), blue(), green(), etc.) appear to be separate,
but the real code in the closure actually was compiled only once.

So, sometimes you might want to use symbolic references to directly
manipulate the symbol table.  This doesn't matter for formats, handles, and
subroutines, because they are always global -- you can't use my() on them.
But for scalars, arrays, and hashes -- and usually for subroutines --
you probably want to use hard references only.
-- 
You want it in one line?  Does it have to fit in 80 columns?   :-)
                --Larry Wall in <7349@jpl-devvax.JPL.NASA.GOV>


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

Date: Tue, 25 May 1999 13:46:31 +0100
From: "Daniel Vesma" <daniel.vesma@thewebtree.com>
Subject: removings " "s from strings
Message-Id: <7ie60g$hmf$1@gxsn.com>

Hi,

How can I remove spaces from the start and ends of strings, I have two
variables, $preOR and $postOR. Either may have spaces at the beginning or
the ends. I need to find some code that looks for the spaces and deletes
them if they are there.

Any ideas? Please? Help. I'm going nuts.

Daniel Vesma
http://www.thewebtree.com
http://www.thewebtree.com/daniel-vesma





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

Date: Tue, 25 May 1999 13:10:35 GMT
From: Eric Bohlman <ebohlman@netcom.com>
Subject: Re: removings " "s from strings
Message-Id: <ebohlmanFCAHxn.GDt@netcom.com>

Daniel Vesma <daniel.vesma@thewebtree.com> wrote:
: How can I remove spaces from the start and ends of strings, I have two
: variables, $preOR and $postOR. Either may have spaces at the beginning or
: the ends. I need to find some code that looks for the spaces and deletes
: them if they are there.

Take a look at "How do I strip blank space from the beginning/end of a 
string?" in perlfaq4.



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

Date: Tue, 25 May 1999 15:24:00 +0200
From: real@earthling.net (Real)
Subject: Re: removings " "s from strings
Message-Id: <MPG.11b4c2d86f16d62a989702@news.surfnet.nl>

Daniel Vesma wrote;
# Hi,
# 
# How can I remove spaces from the start and ends of strings, I have two
# variables, $preOR and $postOR. Either may have spaces at the beginning or
# the ends. I need to find some code that looks for the spaces and deletes
# them if they are there.

This will remove any leading of trailing spaces from $line :
    $line =~ s/^ *| *$//g;

# Any ideas? Please? Help. I'm going nuts.

If I'm not mistaken, this is in the FAQ. To prevent any more braindamage, 
I suggest you to keep it within reach :)

Bye,
Real
 
# Daniel Vesma
# http://www.thewebtree.com
# http://www.thewebtree.com/daniel-vesma


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

Date: Tue, 25 May 1999 14:40:54 +0100
From: "Daniel Vesma" <daniel.vesma@thewebtree.com>
Subject: Re: removings " "s from strings
Message-Id: <7ie9eo$b2c$1@gxsn.com>


>If I'm not mistaken, this is in the FAQ. To prevent any more braindamage,
>I suggest you to keep it within reach :)


Thanks lots, do you mean the URL of the newsgroup. If so, can you point me
to the URL? I don't see the message,

Daniel Vesma
http://www.thewebtree.com
http://www.thewebtree.com/daniel-vesma





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

Date: 25 May 1999 07:55:02 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: removings " "s from strings
Message-Id: <374aabb6@cs.colorado.edu>

     [courtesy cc of this posting mailed to cited author]

Cursed by the disgusting Microsoft Outlook Express 4.72.3110.1, in
comp.lang.perl.misc "Daniel Vesma" <daniel.vesma@thewebtree.com> writes:

:How can I remove spaces from the start and ends of strings, I have two
:variables, $preOR and $postOR. Either may have spaces at the beginning or
:the ends. I need to find some code that looks for the spaces and deletes
:them if they are there.
:
:Any ideas? Please? Help. I'm going nuts.

What part of the standard documentation included with every release of
perl was unclear to you?  Or is it that you don't really have perl at
all, and this is merely a hypothetical question?  The answer is on your
own system.  You don't have to waste the time of the net.  grep for trim
in perlfaq4 and in perlop.  I realize the Microsoft mentality runs 
counter to looking things up on your own system, but cope: this is
our world you're in now.  clpm helps those who help themselves.

--tom
-- 
    "A good messenger expects to get shot." --Larry Wall


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

Date: Tue, 25 May 1999 14:09:50 +0200
From: "Bastiaan S van den Berg" <office@asc.nl>
Subject: req: directional help
Message-Id: <7ie40h$gso$1@zonnetje.NL.net>

hi!

i'm a young internet enthousiast that is starting to look at perl for easy
to maintain , template driven , database-like approaches to normal webpages

what i need is someone who can point me to some information about
programming in perl , some good tutorials , maybe some articles on dbi
(besides the one from tpj , i have that one here :)

plz help me , as my mind get's flooded like a sponge when i just use
altavista with +perl +tutorial +dbi

cul8r
buZz






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

Date: Tue, 25 May 1999 04:35:50 -0400
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: req: directional help
Message-Id: <6dndi7.e6k.ln@magna.metronet.com>

Bastiaan S van den Berg (office@asc.nl) wrote:

: what i need is someone who can point me to some information about
: programming in perl , some good tutorials , maybe some articles on dbi
: (besides the one from tpj , i have that one here :)


   Perl FAQ, part 3:

      "Where can I learn about CGI or Web programming in Perl?"


--
    Tad McClellan                          SGML Consulting
    tadmc@metronet.com                     Perl programming
    Fort Worth, Texas


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

Date: Tue, 25 May 1999 14:58:55 +0200
From: Kaare Rasmussen <kar@webline.dk>
Subject: Return of array
Message-Id: <374A9E8F.2270F562@webline.dk>

Hi

Trying to make an OO module with a subprocedure that assigns a reference
to an array to $self{'var'}

Something like

sub sub {
 my @var = qw(var1 var2 var3);
 $selv->{'var'} = \@var;
}

How can I dereference $selv->{'var'} after sub sub has been called?




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

Date: Tue, 25 May 1999 03:58:59 -0400
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: String search problem
Message-Id: <38ldi7.itj.ln@magna.metronet.com>

Raul R Ramirez (raulr@CS.Arizona.EDU) wrote:

:   Silly question, but....if I have a string stored in $x,
:   how can I find the POSITION of any substring???


   perldoc -f index


--
    Tad McClellan                          SGML Consulting
    tadmc@metronet.com                     Perl programming
    Fort Worth, Texas


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

Date: 25 May 1999 14:41:13 +0100
From: Jonathan Stowe <gellyfish@gellyfish.com>
Subject: Re: Using Perl over server, Help
Message-Id: <374aa879@newsread3.dircon.co.uk>

WebWizard0 <webwizard0@aol.com> wrote:
> Im trying to run my perl script from over a server.  A very simple program
> like;
> 
> #!!/user/local/bin/perl
> $temp = 'test';
> print "$temp";
> 
> 
> Fails when I try calling it from a browser by using the URL;
> http://www.webpage.com/cgi-local/program.pl
> 
> I get internal (500) errors.

Its not entirely surprising that this does not work because it doesnt
make any effort to co-operate with the CGI specification - it needs
to return at least one header.

Why dont you try:

#!/usr/local/bin/perl -w

print "Content-type: text/plain\n\n";
print "Hello, World";

Which should work.

You should really address questions about the CGI to the newsgroup:

comp.infosystems.www.authoring.cgi

/J\
-- 
Jonathan Stowe <jns@gellyfish.com>



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

Date: Tue, 25 May 1999 15:37:47 +0200
From: "D. Emilio Grimaldo Tunon" <emilio_tunon@nl.compuware.com>
Subject: Re: XS programming (HELP!) newbie...
Message-Id: <374AA7AB.D1A9F574@nl.compuware.com>

Didimo Emilio Grimaldo Tunon wrote:
> 
> Hi XS*,
>     I am just beginning to get into Perl Extension programming (XS),
> have read the various manual pages and have gotten something basic
> working but I have the following questions:
> 
> 1. The perlxstut man page says that during 'make install' it is
>    installed in the Perl's system directory (/usr/lib/perl5/...)
>    but what about being able to install it in 'user space'? say
>    $HOME/perllib/ ? is that possible? I would hope so, that way
>    one can try these binary modules before actually installing
>    them on the system (among other applications...)
> 
> 2. I have a Perl function that I invoke like:
> 
>        &SetRequest(\$var1, \@array, \%hash);
> 
>    but I haven't figured out how to get the thing to either
>    let var1 be known to the caller or get elements (in the XS)
>    from the array. The array has strings.
> 
>    Could somebody give me a short example of how to have the
>    proper XS stub for this function? what about getting the
>    string elements of the array? I figured out how to get the
>    length of the array but that's it :( and if it is not much
>    to ask (for the sake of an example) obtaining the key and
>    value of the hash. Both the array and the hash are actually
>    references-to..
> 
>    I think that will give me a quick start, in the meantime
>    I will read the man pages again but I haven't found any
>    useful, practical example out of it...
> 




     All questions still unanswered, the manual pages left
them unanswered as well, web search on Perl+XS did not lead
to anything useful either (only HTML versions of XS manual
pages). Any true XS tutorial out there with real world
examples?

-- 
D. Emilio Grimaldo Tunon       Compuware Europe B.V. (Uniface Lab)
Software Engineer	       Amsterdam, The Netherlands
emilio_tunon@nl.compuware.com  Tel. +31 (0)20 3126 516
*** The opinions expressed hereby are mine and not my employer's ***


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

Date: 12 Dec 98 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Special: Digest Administrivia (Last modified: 12 Dec 98)
Message-Id: <null>


Administrivia:

Well, after 6 months, here's the answer to the quiz: what do we do about
comp.lang.perl.moderated. Answer: nothing. 

]From: Russ Allbery <rra@stanford.edu>
]Date: 21 Sep 1998 19:53:43 -0700
]Subject: comp.lang.perl.moderated available via e-mail
]
]It is possible to subscribe to comp.lang.perl.moderated as a mailing list.
]To do so, send mail to majordomo@eyrie.org with "subscribe clpm" in the
]body.  Majordomo will then send you instructions on how to confirm your
]subscription.  This is provided as a general service for those people who
]cannot receive the newsgroup for whatever reason or who just prefer to
]receive messages via e-mail.

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.misc (and this Digest), send your
article to perl-users@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.

The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.

The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.

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 V8 Issue 5772
**************************************

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