Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

[Solved] Question regarding Perl's "defined" function...

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
4 posts • Page 1 of 1
Author
Message
cgmd
Veteran
Veteran
User avatar
Posts: 1585
Joined: Thu Feb 17, 2005 6:58 pm
Location: Louisiana

[Solved] Question regarding Perl's "defined" funct

  • Quote

Post by cgmd » Fri Mar 09, 2007 2:19 pm

Hi, all...

I hope to teach myself Perl, using "Learning Perl" by Randal L. Schwartz, et al. I have no background writing scripts, so this is all pretty much new to me.

Within the early part of the book, I ran across a script meant to help with the understanding of the defined function:

Code: Select all

#!/usr/bin/perl

$madonna = <STDIN>;
if ( defined($madonna) ) {
  print "The input was $madonna";
} else {
  print "No input available!\n";
}
I'm unable to make this script print out "No input available!"... :? If I run the script, and not enter anything, nothing happens. If I run it and only hit return, I get:

Code: Select all

 cgmd  ~/bin $ defined 

The input was 
cgmd  ~/bin $ 
I'm guessing the return key sends an "end of line signal" to <STDIN> , causing defined to return true, and, therefore avoid the "else" alternative. If that's the case, how can I make this script print out the line "No input available!"??

What am I missing here??

Thanks!
Last edited by cgmd on Sat Mar 10, 2007 2:05 am, edited 1 time in total.
"Primum non nocere" ---Galen
Top
grimm26
Guru
Guru
User avatar
Posts: 313
Joined: Sun May 23, 2004 5:40 am
Location: Chicagoland, IL

  • Quote

Post by grimm26 » Fri Mar 09, 2007 2:48 pm

You're right. The newline that happens when you hit enter is input into the script.
If your goal is to find if someone entered something meaningful (non whitespace). you should go this way:

Code: Select all

while(<STDIN>) {
  if(/\S+/) {
    print "You typed $_";
  }
}
If you just want to make that script tell you it didn't get any STDIN, try running like so:

Code: Select all

 ./defined < /dev/null
Top
vandien
Tux's lil' helper
Tux's lil' helper
Posts: 137
Joined: Wed May 03, 2006 1:56 pm

  • Quote

Post by vandien » Fri Mar 09, 2007 3:29 pm

If that script is straight from the book, I'm not sure I would trust it..

Code: Select all

$madonna = <STDIN>;
chomp $madonna;
if ($madonna) {
  print "The input was $madonna\n";
} else {
  print "No input available!\n";
}
Note, the three things that changed. I added chomp. This safely removes the newline from the user input. This is important to use on input from stdin, files, or system commands.

I took out the defined bit. I'm not sure how it's supposed to work (I never use it), but it obviously doesn't in this example. Testing the variable itself tells you whether it has data in it or not.

The last change was a minor cosmetic fix, adding a newline to make the output look better.
Top
cgmd
Veteran
Veteran
User avatar
Posts: 1585
Joined: Thu Feb 17, 2005 6:58 pm
Location: Louisiana

  • Quote

Post by cgmd » Fri Mar 09, 2007 6:20 pm

grimm26 wrote:
If you just want to make that script tell you it didn't get any STDIN, try running like so:
Code:
./defined < /dev/null
That's exactly the information I needed...

Not entering keystrokes into <STDIN>, but rather sending "/dev/null" to the script, as you have shown, produces the output I was seeking! :)

I also appreciate the alternative script suggestions from both posts...

Thanks!
"Primum non nocere" ---Galen
Top
Post Reply

4 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic