← Back to Perl Course | Chapter 1: Introduction | Lesson 7 of 7

Perl के One-liners

-e switch आपको सीधे command line से एक छोटा Perl program चलाने देता है।

In this page:

  1. Perl One-liners

Perl One-liners

quoted code के बाद perl -e इसे बिना किसी file के चलाता है। Useful switches में शामिल हैं -n (हर input line पर loop करना), -p (loop करना और हर line print करना), -l (आपके लिए newlines handle करना), और -i (files को in place edit करना)। One-liners shell pipelines में quick text transformations के लिए popular हैं।

Note: one-liners में warnings देखने के लिए -w इस्तेमाल करें या code के अंदर use warnings डालें।

उदाहरण: Perl one-liners

bash
$ perl -e 'print "Hello from a one-liner\n"'
Hello from a one-liner

$ echo "perl is fun" | perl -pe 's/perl/Perl/'
Perl is fun

$ perl -lne 'print length' words.txt
$ perl -i.bak -pe 's/cat/dog/g' notes.txt

⚠️ Run this in your own terminal or Node.js environment.

{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. code के चारों ओर quotes भूल जाना ताकि shell इसे split कर दे
  2. -n (कोई automatic print नहीं) और -p (हर line print करता है) को mix up करना
  3. ऐसी files पर बिना backup suffix के -i इस्तेमाल करना जिन्हें आप recover नहीं कर सकते
चैप्टर सारांश
  • perl -e command line से code चलाता है
  • -n input lines पर loop करता है
  • -p loop करता है और हर line print करता है
  • -i files in place edit करता है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.