Re: Net::SFTP causes script to exit if connection can't be made
Zentara is right about what you need to do, but a fuller explanation of the solution would have been: Net::SFTP is throwing an exception when it cannot make the connection. If the exception is not caught, it will cause your program to exit. The way you catch exceptions in Perl is to wrap the code that throws them in an eval block. After the eval block, the exception will be stuffed into the $@ variable, so you can examine that variable to see if there is some kind of useful information there about what the exact problem was. --Dks On Sat, Dec 14, 2002 at 09:50:33AM -0500, zentara wrote: On Fri, 13 Dec 2002 11:10:54 -0500, [EMAIL PROTECTED] (Ian Zapczynski) wrote: Unfortunately, using the || as below doesn't change the behavior. Once my script tries to make the connection and can't, it exists with a connection failed to $host, etc. etc. message, whether I use warnings or diagnostics or neither. If anyone can help me understand why this may be or if they experience the same, this would be terribly helpful. As I mentioned, I need to be able to trap this error and continue processing if a connection fails. Wrap it in an eval statement. ##3 #!/usr/bin/perl -w use strict; use Net::SFTP; my $sftp = undef; eval{ $sftp = Net::SFTP-new(localhost, user=zzztester, password=zzztest, ); }; if ($@) { print Sftp connection failed:\n $@\n; } if (! $sftp) { print I can't connect!\n; }else{ print SUCCESS!\n; } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Why does this code fail? - Sorry here are the line numbers..
On Sat, Dec 14, 2002 at 12:51:50PM -0500, [EMAIL PROTECTED] wrote: Can somebody tell what is wrong with this. I tried single quote (') in place of double quote() neither of them work. 184 for ($i = 0; $i @clm_types; $i++) 185 { 186@temp_str = grep (/$_/,@env_desc); 187@fields = split (/,/,$temp_str[@temp_str - 1],4); 188$env_final[$i] = join (/,/,$fields[1],$fields[2],@temp_str - 1); 189 } /,/ should probably be written as , at loadstats1.pl line 188. Unlike split, join() takes a string as its first argument, not a regex. What you want is this: $env_final[$i] = join (,,$fields[1],$fields[2],@temp_str - 1); | Notice--no '/'s here However, I'm not really sure that that's what you want either. As written, this line will join three elements into a string and assign it to $env_findal[$i]. The three elements it will join are: $fields[1] $fields[2] @temp_str - 1 The problem is that last one...I suspect that you want it to be the last element of @temp_str, but what it is actually going to be is the number that you get by subtracting 1 from the number of elements in @temp_str. Try this instead: $env_final[$i] = join(,,$fields[1],$fields[2],$temp_str[-1]); --Dks -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
context of function calls
Hey folks, Executive summary: It looks like, no matter what context foo() is called in, its arguments are always evaluated in list context. Is this correct? I had always understood that: - a function's arguments were evaluated in the same context as the context of the function (hmm...actually, thinking about it, this seems like it can't be right. Is it simply always LIST, or can it vary?) - a function's context propogated downwards into any functions it calls. Full version: I thought I had a pretty good handle on the whole concept of context, but I've tripped over it several times recently, so I set up a test: #!/usr/local/bin/perl -w use strict; $\ = \n; {print \$x = bar(); my $x = bar(); foo($x);} {print \@x = bar(); my @x = bar(); foo(@x);} {print \$x = foo( bar() ); my $x = foo( bar() );} {print \@x = foo( bar() ); my @x = foo( bar() );} {print foo( bar() ); foo( bar() );} sub foo { while (@_) { my $parm = shift; for ($parm) { unless (defined) { $parm = 'VOID'; last; } $parm = $parm ? 'LIST' : 'SCALAR'; } print \t$parm; } } sub bar { return wantarray(); } print Done.; __END__ This outputs what context bar() is called in. I expected this to output the following: ### EXPECTED OUTPUT. THIS IS NOT WHAT IT GIVES $x = bar() SCALAR @x = bar() LIST $x = foo( bar() ) SCALAR @x = foo( bar() ) LIST foo( bar() ) VOID Done. ### /EXPECTED OUTPUT. Instead, I get this: $x = bar() SCALAR @x = bar() LIST $x = foo( bar() ) LIST @x = foo( bar() ) LIST foo( bar() ) LIST Done. Any comments, anyone? Dave -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: aritmetic operators
Hi Matthew, First of all, subtracting scalars is perfectly valid, so the actual error must be something else. Here are three things you should check. 1) First, you don't have a semicolon at the end of that line. 2) Second, if you are operating under 'use strict', you will need to predeclare your variables before using them so, (assuming that $dif and $score were declared elsewhere): my $need = $dif - score; 3) A 500 server error implies that you are writing a CGI. I can't tell based on what you've said, but I think the most likely thing that is happening here is that, somehow, what you are generating invalid HTML and your web server is spitting up on the results...in other words, this is a CGI error, not actually an arithmetic error. HTH, Dave On Sun, 24 Mar 2002, Matthew Harrison wrote: I have two scalars created from passed params. i want to create a third variable by subtracting the two scalars. how can i do this? i have tried: $need = $dif - $score but i get a 500 server error and my error log says that i cannot use the '-' operator on a scalar variable. can anyone suggesta better way? cheers -- Matthew Harrison Internet/Network Services Administrator Peanut-Butter Cheesecake Hosting Services Genstate www.peanutbuttercheesecake.co.uk -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Sort of Conceptual, Syntax, and modules related questions
Hi Connie, On Sat, Mar 23, 2002 at 05:30:25AM +0800, Connie Chan wrote: 1. When I open a text file with *lines, however, I just want to read the first line, would this be a good idea ? open (FILE, textfile.txt); $firstline = FILE; close (FILE); would this process run till EOF, or terminated after read $firstline ? Short, oversimplified answer: - In scalar context, FILE will read everything up to the next newline. - In array context, FILE will read all the entire file and return a list. First first element in the list will be the first line in the file, the second list element will be the second line, and so on. Answer-that-is-probably-more-complete-than-you-need: When I said that it would read up to the next newline, that was a fib. In actuality it will read everything up to and including the next input record separator. The input records separtor (IRS), is stored in a variable called $/ (or $IRS, or $INPUT_RECORD_SEPARATOR, if you are using the English module), and by default it is newline (\n on Unix, which is automatically translated into \r\n on Win32). 2. I am using Win32 system , would you tell when is \n apprear and when is \r\n apprear ? What about this when dealing with binary files ? Perl is a helpful language, it tries to DWYM (Do What You Mean) whenever possible. So, this line of code: print \n; will print a newline on any platform...on Unix, it will print a single LF (Linefeed, ASCII character 10). On Mac, it will print a CR (Carriage Return, ASCII code 13). On Win32, it will print a CR followed by a LF. As to what about binary files... If you are writing out a binary file, first open it, then, call 'binmode(FILE)', and then just write to it normally. The conversion of \n to \r\n will not happen at this point--which is what you want. If you are reading a binary file, you probably want the read() function (perldoc -f read for details). \n conversions are not done 4. Is that reading binary data faster then reading text file ? Your question is a little vague, but I think the answer is no. If you read 100 bytes from a file, it shouldn't make any difference whether that file in binary or text. The limiting factor is the speed of the disk, not anything about your OS or about perl. 6. Is that any syntax for local call about 'use' and 'require', so as something like 'not require', 'not use' expressions ? I think what you're asking here--correct me if I'm wrong--is whether or not 'use' and 'require' can be turned off within a certain scope. Short answer: you cannot turn off 'require', but you can turn off 'use', as long as what you were using was a pragma. You do this by saying no strict; or whatever the pragma name is that you want to turn off. The pragma remains off until the end of the current lexical scope. Long answer: First, let's talk about what these constructs do. 'require' does one of two things: 1) require 5.005mandates that the interpreter be at least version 5.005, or it should stop right here. 2) require Foo::Bar tells the interpreter to go read and eval the contents of the file Foo/Bar.pm, which are to be found somewhere on one of the paths listed in @INC. Clearly, you can't 'no require' something. Once you have done a require 5.005, your interpreter either measures up (and keeps running) or it doesn't (and it exits with an error message). Alternatively, once you have read in a file, there is no (quick/easy) way to get rid of it. 'use', on the other hand, does the following. 1) use 5.005means exactly the same as the require version. Again, there is no way to turn this off after the fact. 2) use Foo::Barthe same as require (mostly *). Again, cannot be turned off after the fact. 3) use strict activates a pragma, which controls how the Perl interpreter does things. For example, strict makes it a fatal error to use symbolic references, to use variables without declaring them, and to call subroutines without prefacing them with '' or suffixing them with (). You can turn off a pragma as described above. It works like this: use strict 'vars'; # using an undeclared variable causes error $foo = 7; # this would be an error { no strict 'vars'; $bar = 8; # this would NOT be an error some_func(); } $baz = 9; # this would be an error sub some_func { $jaz = 9; # this would be an error } Note that the 'no {pragma}' only works in the lexical scope, not the dynamic scope--that means that 'strict' is turned off when you are dealing with $bar, but that that turned-off state does not propogate down into some_func(). 7. Would you give me some short
Re: tie/bless interactions?
Peter and Jenda, Thank you both for your information and pointers, I appreciated them. I actually ended up doing something slightly different; I wanted to make method calls use identical syntax, whether they were going to the APC::Event or the Tie::DBI, so what I ended up doing was giving the APC::Event a has-a relationship with the Tie::DBI, and then delegating (through AUTOLOAD) calls to the Tie::DBI. It ended up working great! Dave Storrs -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
tie/bless interactions?
I've got a class called APC::Event. The idea is that when you instantiate one of these objects, you are signalling that a particular event has happened; ergo, it takes the information you specified, deduces a bunch more stuff, and logs all of it to a database. Afterwards it sticks around and provides an interface to that record (or the whole table, actually). I've just discovered the Tie::DBI modules, and they seem like exactly what I want here, but I'm wondering if the following is going to get me in trouble: sub new { my ($proto, %args) = @_; my %self; my $database_handle =$args{ dbh } || DBH(\%default_database_params, {PrintError = 0, RaiseError = 1} ); tie %self, Tie::DBI, { db = $database_handle, CLOBBER = 1, # Allow INSERT and UPDATE, # but not DELETE }; my $class =ref $proto# Are we being called as a class or || $proto# instance method? || 'APC::Event'; bless \%self, $class; } I haven't done that much with tie in the past, and I've never done anything where I tied an object and then blessed it, so I'm not quite sure what the implications are. Specifically, 1) As I understand it, tie() uses bless() under the hood; is this correct? 2) I want the object to be an APC::Event, because there is more in the class than just a constructor. Assuming that the answer to question 1 is yes, what happens when I rebless a reference? I believe it forgets all about its original class, but I've never done it before (actually, I've tried very hard to avoid it). 3) If I bless %self out of the Tie::Hash space and into the APC::Event space, are the Tie::Hash functions going to stop working? (I assume so.) 4) If the answer to 3 is yes, can I solve this by doing this: my $rh_self = \%self; unshift @{$rh_self-ISA}, 'APC::Event'; 5) Given the above constructor code, am I likely to have any problems with circular references? Thanks in advance, Dave Storrs PS If you wanted to rewrite the code snippet in question 4 without taking a reference, how would you do it? This doesn't seem right: unshift @self{ISA}, 'APC::Event'; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Test Automation
Check CPAN for: Test::Unit Test::Simple Test::More and a few others. Dave On Mon, 26 Nov 2001, Ahmed Moustafa Ibrahim Ahmed wrote: Hi Everybody, How can I automate the process of testing my Perl script? Is there a standard way to do? Is there a tool for testing Perl scripts? Your help will be appreciated so much. Ahmed -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: directories
Any of the following should work for you: if ( -e $dirpath -d $dirpath ) { ...stuff...} if ( -e $dirpath -d _ ) { ...stuff...} if ( -d $dirpath ) { ...stuff...} You can always substitute $dirpath for a literal string path in the above as well...e.g.: if ( -d '/usr/bin/home' ) { ... stuff ... } The -e test checks to see that $dirpath exists, -d checks that it is a directory. The underscore means whatever the last filetest operated on, operate on that same thing. Read more about it here: http://www.perldoc.com/perl5.6/pod/func/X.html Dave S On Wed, 21 Nov 2001 [EMAIL PROTECTED] wrote: what is the best way to check for the existence of a directory? (windows/Linux) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Modules
In practical daily use, use(); is preferred as since it compiles the module as soon as it sees 'use Foo::Bar;' before moving on, this will catch errors and scope conflicts far sooner than if you use require(); There aren't many good reasons to use require, at least I can't think of any. When in doubt, use use(); :) If you see a lot of 'requires' in subroutines, I suspect someone just didn't get scope or wrote a quick hack so performance is the least of the codes worries. require() is handy inside an eval { } block to trap whether a module is installed or not. Many CPAN modules use this technique. Very occasionally I will also do something like this: my $needed_file = deduce_where_necessary_code_is(); require $needed_file; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: capture song title
On Fri, 16 Nov 2001, KeN ClarK wrote: I want to capture the title of the current song I have running on my box through mpg123 and redirect it to a file. In this process, I don't want the /long/path/to/song but just the song.mp3. Is it possible to capture the song title this way? Ken $filepath = reverse filepath; my ($title, undef) = split /\//, $filepath, 2; $title = reverse $title; Or, if your supply of whitespace is running low: $title=reverse shift @{[split(/\//,reverse($filepath),2)]}; But you probably don't want to do that if you'll ever need to look at this code again. : Dave -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: What was your first PERL script (was Off-Topic (200%) - Whereare you from?)
Mine was to write a document-management system for Paine Webber. Of course, I had the advantage of working with two or three other Perl programmers, all of whom were substantially skilled. Still, that was how I first got exposed to the language. (AFAIK, PW is still using that system; they hired the original team, including me, back a year later to expand it. It was pretty cool too...it could manage 100,000s of docs, could do fax, email, etc. And this was all back in 1996 or so.) Dave On Tue, 13 Nov 2001, Dave Turner wrote: Mine just got completed after about 6 months of on and off work. It is a CGI app to allow a merchant to update items for sale via a web page. Adds them on one side, and check boxes allow you to choose which to get rid of on the other side. It writes items to a small file -- it's too small a number to use any kind of real database, but then when it's called up from their site, it dynamically builds the html for the user from that file. And I'm still learning... :-) ( I want to be able to add pics to this app next... lol ) At 03:54 PM 11/11/01 -0500, you wrote: Mine was only about 4 months ago and it was an online shopping catalog browser. Good thing I only had to link into shopping cart and credit card verification code! -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] Sent: Sunday, November 11, 2001 8:43 AM To: [EMAIL PROTECTED] Subject: Re: What was your first Perl script (was Off-Topic (200%) - Where are you from?) Brett == Brett W McCoy [EMAIL PROTECTED] writes: Brett My first Perl script, from 1998 or 1999 My first Perl script was from a decade earlier than that. You young'uns. :) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Updating a hash using a refernece?
On Tue, 13 Nov 2001, AMORE,JUAN (HP-Roseville,ex1) wrote: How do I update the value pointed to by key PAGER from more to pg. when using a reference only to the hash element for key PAGER. %Unix= (SHELL = /bin/csh, PAGER = more, DB = mysql); print Value: , $unix{PAGER}; Are you saying that you have a reference to the hash itself, or a reference to the scalar element stored in the hash? See below: my %hash = ( SHELL = '/bin/csh' ); my $rh_hash = \%hash; my $rs_element = \($hash{SHELL}); print $hash{SHELL}, \n; $rh_hash-{SHELL} = 'blog'; print $hash{SHELL}, \n; $$rs_element = 'wurzle'; print $hash{SHELL}, \n; Outputs: /bin/csh blog wurzle Dave -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Off-Topic (200%) - Where are you from?
At the moment, New York, a.k.a. Terrorist Target Number #1 :/ Dave -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: installing modules
On Tue, 13 Nov 2001, Rahul Garg wrote: i have actually got the solution. if i have the module Bar.pm in directory '/usr/home/rahul/myMod' then in the perl script that uses this module i have to include the statement use lib '/usr/home/rahul/myMod' ; use Bar ; Thanks, Rahul Yep, that will certainly do it, and is often the best way. Another way to do it described here (ref PERL5LIB and PERLLIB vars): http://www.perldoc.com/perl5.6/pod/perlrun.html#ENVIRONMENT Dave -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: vars as key/value in a hash
FYI, Tyler, the reason that this will do what you want is because you're original program wasn't doing what you thought. %stations = ( $names[0] = $names[1] ); This creates a list of two elements and assigns it to the hash named %stations. Therefore, $names[0] becomes the one and only key in %stations and $names[1] becomes the one and only value. If %stations did not exist before, it does exist now; if it _did_ exist, then its former values are wiped out. That's the key point...every time you went through the loop, you were wiping out all the previous data you had read in, so the table only ever contained one key and one value. Therefore, since (I assume) 'alta_guard' was not the last entry in the file you were reading from, it did not appear in the table when you were finished. Notice that Brett's version saves the values from one iteration of the loop to the other, because you aren't reinitializing the hash. HTH, Dave On Thu, 8 Nov 2001, Brett W. McCoy wrote: On Thu, 8 Nov 2001, Tyler Cruickshank wrote: open(NAMES, d://perl/avalanche/names.txt) || die Cant open names.txt\n; while(NAMES){ chomp; @names = split(/-/); %stations = ( $names[0] = $names[1] ); @names = (); } # End while. $table = 'alta_guard'; print Text: $stations{$table}\n; I would do it like this: %stations = (); open(NAMES, d://perl/avalanche/names.txt) || die Cant open names.txt\n; while(NAMES){ chomp; @names = split(/-/); $stations{$names[0]} = $names[1]; } # End while. $table = 'alta_guard'; print Text: $stations{$table}\n; -- Brett http://www.chapelperilous.net/ To give happiness is to deserve happiness. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: SecureCRT + Perl
Um...not sure what you're asking for here. I use SecureCRT all the time (using it right now, in fact), and I do in fact write Perl while securely telnetted into various machines. What do you want to do? Dave On Thu, 8 Nov 2001, A. Rivera wrote: Has anyone tried to use SecureCRT with Perl? If so, any examples scripts that interact with a shell, etc.? Much appreciated, A.Rivera -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: installing modules
Since I suspect your next question would be Ok, then how do I install the CPAN module? let me head this one off. The CPAN module (CPAN.pm) comes with the basic distro of Perl, so you should already have it. Say you want to install the CGI.pm module from CPAN. At your command line, you type: perl -MCPAN -e 'install CGI' The -MCPAN means use a module (-M), specifically, CPAN.pm. The -e means the script will be specified on the command line. If this is the first time you've ever used CPAN, it will need to ask you a LONG set of questions...don't be intimidated, it provides default answers to pretty much everything, and the defaults are generally correct. However, your question seems to have been about how to install your OWN modules. Do this: perl -e 'print join \n, @INC, \n;' This will dump out a list of directories. On my system, the list is this: /usr/local/lib/perl5/5.6.1/alpha-dec_osf /usr/local/lib/perl5/5.6.1 /usr/local/lib/perl5/site_perl/5.6.1/alpha-dec_osf /usr/local/lib/perl5/site_perl/5.6.1 /usr/local/lib/perl5/site_perl/5.6.0/alpha-dec_osf /usr/local/lib/perl5/site_perl/5.6.0 /usr/local/lib/perl5/site_perl . Now, if I put a file named Foo.pm in /usr/local/lib/perl5/site_perl, I could then tell Perl to use that file as a module in one of my scripts by putting the following line at the top of the script: use Foo; If I created a directory in /usr/local/lib/perl5/site_perl named 'Foo/' and in that directory I put a file named 'Bar.pm', then I could use that module by doing: use Foo::Bar; Hope this helps. Dave On Fri, 9 Nov 2001, Martin Pfeffer wrote: Install the CPAN Module Then type install Modulname Hope it helps martin Rahul Garg wrote: well, i want to install modules on my system(linux) that i have made. what i know is they are to be installed at @INC. but exactly how.any suggestions any sources for reading... Bye, Rahul -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: regexp with $ARGV
On Mon, 5 Nov 2001, Martin Karlsson wrote: Thanks a lot for your help and your time! I think I've got it solved now. You're welcome. : Could any of you recommend a good book for (learning) Perl? There seems to be quite a few to choose from... Oddly enough, that's what it's called: _Learning Perl_ http://www.oreilly.com/catalog/lperl3/ It's by Randal Schwartz Tom Phoenix. The target audience is people who already have basic programming knowledge and just need to learn Perl. However, I've heard people say that it also served as a good introduction to programming in general. Dave -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
OT: dB pooling in Perl (was Re: Perl with Java)
On this subject: I am currently trying to sell Perl in my (highly Java-biased) shop, and having an uphill battle of it. One of the things that the brass wants to see is support for database connection pooling in a multi threaded environment. Now, I'm sure that Perl can do this, but it isn't something I've done before, so I don't have a ready answer on where to look. Mason? I know that version 2 of Apache/mod_perl will provide this, but that isn't out yet and who knows when it will be. Any suggestions on where else to look? Dave On Sat, 3 Nov 2001, Greg Meckes wrote: We use Perl and Java in our shop. I'm the Perl guy and I haven't encountered any situation where I couldn't code up something to do same thing that the Java apps do. That's not to bring into it any 'preference' of one language over the other as they both have their usability goods and bads. We experience a variety of problems using Java because of it's security lockdown coding style - which is a good thing - but makes for longer dev/debug time. It's easy to code things up in Perl and get them to do the same thing. These issues came be overcome I'm surebut this is what we have been facing. greg --- pc [EMAIL PROTECTED] wrote: How widely is Perl Used in Java Shops? Pc _ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] __ Do You Yahoo!? Find a job, post your resume. http://careers.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: regexp with $ARGV
Martin, I'm not entirely clear on what you're trying to do here, so if this doesn't help, let me know and I'll try again. I think the problem is that you're doing this: s/$ARGV[0]/\($ARGV[0]\)/g ...when you want to affect $ARGV[0]. But remember that s/// and m// are, by default, applied to the contents of the $_ variable. So, when Perl sees that line of code above, it effectively rewrites it to be: $_ =~ s/$ARGV[0]/\($ARGV[0]\)/g; Which, in English, means something like this: Go through the string stored in the $_ variable. Look for a substring which is identical to the string stored in $ARGV[0]. If you find it, replace it with that same string with parens around it. Finally, because there is a 'g' option on the operation, do not stop after finding the first match; continue through the contents of $_ and replace any other matches you find. I think what you want instead is this: $ARGV[0] = ($ARGV[0]); Dave On Sun, 4 Nov 2001, Martin Karlsson wrote: Could anyone please show me the way to think here? If I execute a script with an argument, e.g monkey, then monkey will be found in $ARGV[0]. If I then want to highlight the word monkey by putting it in parentheses, i thought something like s/$ARGV[0]/\($ARGV[0]\)/g would do the trick; however it won't. Thanks, -- Martin Karlsson [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
(Slightly OT) RE: terminating input
On Sun, 4 Nov 2001, Gary L. Armstrong wrote: I am amazed. How does someone figure out that you can do this sort of thing? chomp($value[++$i] = STDIN); I mean, $value[++$i]? That really works? Crazy. [...] Well, that's mostly a C-style issue (and yes, it is crazy). C programmers, for some bizarre reason, are convinced that there is a tremendous shortage of whitespace in the world, and so, rather than contribution to the death of the Old Growth Whitespace forests, they will go to incredible lengths to jam things onto one line. Personally, I have this thing for clarity. In my not-even-marginally-humble opinion, a line of code should do one thing and one thing only (ok, sometimes I stretch a point and do two things, if it is efficient and idiomatic). In particular, I have always despised the increment a variable inside an array index idiom in C. Therefore, I would have written it as: STDIN; chomp; $i++; $value[$i] = $_; Some people say Oh no! Look at that! You took up 4 lines where only 1 was needed! Shame on you! Gasp, horror, collapse in revulsed convulsions. To which I shrug. They always thank me later, when they need to maintain my code. Rant mode off. We know return you to your regularly scheduled list. Dave -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: merging two hashes together?
In the example you provide, this will work: -START %h1 = (one = 1, two = 2, three = 3); %h2 = (four = 4, five = 5, six = 6); # Note that the '' on function calls is optional, unlike $, @, and % $ref_h3 = mergehash(_, _); sub mergehash { my ($rh_first, $rh_second) = @_; { %$rh_first, %$rh_second }; # POINT A } -FINISH The line labelled 'POINT A' does all the work: it constructs a new, anonymous hash reference, initializes it, and (since it is the last value in the function) returns it. Basically, we dereference the two hash references back into hashes, unroll them into lists of key/value pairs, and use them to initialize the new hash we are building. Note, however, that if %h1 and %h2 share any keys in common will end up with the value of whichever hash you list LAST in the hashref. This problem is without solution within the parameters given; no matter what you do, Perl's builtin hash type cannot have duplicated keys, and no key may have more than one value. Now, you can get around it in a LOT of ways...you can use array references to store your values (meaning that one key can hold as many values as you want, hidden inside the array ref), and/or you can use fancy object-oriented magic to make a magical data structure that pretends to be a hash but can have duplicate keys. HTH, Dave On Sat, 3 Nov 2001, AMORE,JUAN (HP-Roseville,ex1) wrote: Hello Perl Gurus, I'm trying to write a program that will merge two hashes together onto a third hash called %h3. I need to return a reference to %h3 to the main program and print each of the values of the hash %h3 using the arrow notation. Name of the actual function performing the merge mergehash. Pass %h1 and %h2 to mergehash as references. The function call in the main program will be something like the following where $ref_h3 will be used to print the values in %h3. $ref_h3 = mergehash(_, _); %h1 = (one = 1, two = 2, three = 3); %h2 = (four = 4, five = 5, six = 6); Many Thanks!!!:) JA -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: HOw do I create a package:
With all due respect, this list is here to help beginner perl programmers deal with programming problems, not to do people's homework for them. (The 'hp.com' address (which prominently dispalys an ad for hp's online university) is a bit of a giveaway.) Read this: perldoc perlmod Dave On Sat, 3 Nov 2001, AMORE,JUAN (HP-Roseville,ex1) wrote: Hello, CAn anyone show me how to create a package called StringPrint and place this package in a filename called StringPrint.pm using a subroutine, called print_str, that prints a string passed to the subroutine defined by the module. Many Thanks! JA -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Access MS SQL using DBI / DBD
Hi Pathi, There is an excellent book from O'Reilly _Programming the Perl DBI_. It has a leopard on the cover, and it should answer every question you could possibly desire concerning the DBI. HTH, Dave On Tue, 30 Oct 2001, Erramilli, Pathi (P.) wrote: Hi, I am new to perl...I am trying to use DBI / DBD to access MS SQL database and I cannot find any documentation/help. What I need to do is to login to the SQL server and get the database space details.(MS SQL 7.0 2000) Can someone help...Thanks in advance. Pathi -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Anyone know to how use subroutines to add or multiply numbers :How would I modify my file:
On Wed, 17 Oct 2001, AMORE,JUAN (HP-Roseville,ex1) wrote: #!/usr/bin/perl -w my $function = shift @ARGV; if( $function =~ /add/ ) { $rtn = add( @ARGV ); print The sum is :$rtn; } elsif( $function =~ /multiply/ ) { $rtn = multiply( @ARGV ); print The product is :$rtn; } sub add { my @list = @_; my $sum = 0; $sum += $_ foreach (@list); } # = This was missing sub multiply { my @list = @_; my $prod = 1; $prod *= $_ foreach (@list); } Dave Storrs -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: XMLParser and Perl
Go to http://search.cpan.org/ and type XML into the search box. There are a whole bunch of XML modules that will make your life much easier. In general, go to CPAN and look for a module is the answer to most problems in Perl...the number of modules is HUGE, and no matter what you need, there is probably a module that can help. Always check CPAN before writing it yourself. Dave On Thu, 18 Oct 2001, Trent A Stephens wrote: I (a beginner in Perl) am looking for guidance on XML and Perl. I am having to read in an XML file, parse it for specific information (MIME type, attributes and values), write the attributes/values to a hash (I am assuming) and then use win32::OLE to persist the attribute/vlaues to the custom properties of a Word or Excel document. Also it would be nice to beable to determine the number of attribute/value pairs in the XML file so as to presize the hash table. Are there any example out there or code I can use to get a good start to speed up my learning curve? There is a slight time constraint I am working with. Thanks in advance Trent -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Unable to display output
On Wed, 17 Oct 2001, nafiseh saberi wrote: in perl , if u want see in screen must write : print \n; before things you want to to print. I mean that you must print one empty line and then print things u wants __ Best regards . Nafiseh Saberi Um, what? Maybe I'm misunderstanding you, but it simply isn't true that you must print an empty line before you can print data to the screen. Here are two ways to prove this to yourself: From the command line: perl -e 'print Hello, World!'; Or, put the following in a file: #!/usr/bin/perl print Hello, World!; Make sure execute permissions are set on the file, then run the file. You will notice that neither of these examples includes a newline (\n) anywhere, yet they still print to the screen. Dave -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]